在我的ViewModel中我对.setValue
的使用是否正确?在这种使用情况下,.postValue
是更可取的吗?
public interface RxSingleSchedulers {
RxSingleSchedulers DEFAULT = new RxSingleSchedulers() {
@Override
public <T> SingleTransformer<T, T> applySchedulers() {
return single -> single
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
RxSingleSchedulers TEST_SCHEDULER = new RxSingleSchedulers() {
@Override
public <T> SingleTransformer<T, T> applySchedulers() {
return single -> single
.subscribeOn(Schedulers.trampoline())
.observeOn(Schedulers.trampoline());
}
};
<T> SingleTransformer<T, T> applySchedulers();
}
public class MasterFragmentViewModel extends ViewModel {
private static final String TAG = "MasterFragmentViewModel";
private final RxSingleSchedulers rxSingleSchedulers;
private final TMDbRepository repository;
private CompositeDisposable disposable;
private final MutableLiveData<List<DiscoverMovie>> movies = new MutableLiveData<>();
private final MutableLiveData<Boolean> error = new MutableLiveData<>();
private final MutableLiveData<Boolean> loading = new MutableLiveData<>();
@Inject
public MasterFragmentViewModel(TMDbRepository repository, RxSingleSchedulers rxSingleSchedulers) {
this.repository = repository;
this.rxSingleSchedulers = rxSingleSchedulers;
disposable = new CompositeDisposable();
}
public void init() {
fetchMoviesByReleaseDate();
}
private void fetchMoviesByReleaseDate() {
loading.setValue(true);
disposable.add(
repository.getMoviesByReleaseDate()
.compose(rxSingleSchedulers.applySchedulers())
.subscribeWith(new DisposableSingleObserver<DiscoverMovieResponse>() {
@Override
public void onSuccess(DiscoverMovieResponse discoverMovieResponse) {
error.setValue(false);
movies.setValue(discoverMovieResponse.getMovieList());
loading.setValue(false);
// Log.d(TAG, "onSuccess: " + discoverMovieResponse.getTotalResults());
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError: " + e);
error.setValue(true);
loading.setValue(false);
}
})
);
}
public LiveData<List<DiscoverMovie>> getMoviesByReleaseDate() {
return movies;
}
public LiveData<Boolean> getError() {
return error;
}
public LiveData<Boolean> getLoading() {
return loading;
}
@Override
protected void onCleared() {
super.onCleared();
disposable.clear();
Log.d(TAG, "onCleared: disposable cleared!");
}
}