正确使用setValue和postValue

时间:2020-02-04 17:19:59

标签: android mvvm rx-java2 viewmodel android-livedata

在我的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!");
    }
}

0 个答案:

没有答案