简体   繁体   中英

Proper way to setup ViewModel with LiveData and Repository with Retrofit

Currently I use a service to make network calls with retrofit but I want to update this model to use ViewModel with LiveData . The thing I am struggling with is how to setup the Repository to update the livedata object.

In the examples I have seen, people return a LiveData wrapped object in the repository something like this

public LiveData<NewsResponse> getData(){
 final MutableLiveData<DataResponse> data = new MutableLiveData<>();
    apiService.getData().enqueue(new Callback<DataResponse>() {

        @Override
        public void onResponse(Call<DataResponse> call, Response<DataResponse> response){

            if (response.isSuccessful()){
                data.setValue(response.body());
            }
        }

        @Override
        public void onFailure(Call<DataResponse> call, Throwable t) {
            data.setValue(null);
        }
    });
    return data;
}

then in the ViewModel they would do

private MutableLiveData<DataResponse> dataResponse = new MutableLiveData();
private Repository repository;

public PopularGamesViewModel(@NonNull Application application) {
    repository = new Repository();
    dataResponse = repository.getData();
}

public MutableLiveData<DataResponse> getData(){
    return dataResponse;
}

Then in the Activity somewhere they would do

viewModel.getData().observe(this, dataResponse -> {
        if (dataResponse != null)
        {
            // Do something
        }
    });

And the thing that seems wrong to me with that is anytime I want to get new/updated data from the repository a new LiveData object is created so previous observers wont work anymore so I would also have to set the observer again right?

How could you set it up so that you just constantly observe a LiveData object and then from the ViewModel call the repository to get any new data and then the ViewModel updates the LiveData object from the Repository ?

Does what I am suggesting make any sense?

As far as I understood your question you want to observe the change in your livedata. for that you might want to refer to MediatorLiveData.

 public LiveData<PagedList<FooPojoList>> liveData;
 private MediatorLiveData<PagedList<FooPojoList>> mediatorLiveData;
 public FooListViewModel(@NonNull Application application) {
super(application);
mediatorLiveData=new MediatorLiveData<>();

}
public MediatorLiveData<PagedList<FooPojoList>> init(FooDao fooeDao,FooFrom foofrom,String orderBy) {
liveData = new LivePagedListBuilder(fooDao.getAllFooList(sqliteQuery), PAGE).build();
    mediatorLiveData.addSource(liveData, new Observer<PagedList<FooPojoList>>() {
        @Override
        public void onChanged(@Nullable PagedList<FooPojoList> fooListPojos) {
            mediatorLiveData.setValue(fooListPojos);
        }
    });
return mediatorLiveData;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM