我已经使用 RxJava 以及 Retrofit 和 Mosby/Conductor 创建了一个应用程序。现在我有这个关于视图处理及其相关内容的问题。
考虑以下代码。
public class IndexPresenter extends MvpBasePresenter<IIndexView> {
@Inject
VideoService videoService;
private CompositeDisposable compositeDisposable = new CompositeDisposable();
private String lastVideoId;
public IndexPresenter(Context context) {
// Dagger Inject
}
@Override
public void detachView(boolean retainInstance) {
compositeDisposable.clear();
super.detachView(retainInstance);
}
public void getTimeline() {
Disposable disposable = videoService
.getTimeline(null)
.delay(5, TimeUnit.SECONDS)
.compose(Util.schedulers())
.doOnSubscribe(__ -> getView().startLoading())
.doFinally(() -> getView().stopLoading())
.doOnError(throwable -> Timber.d(throwable, "failed getting timeline data"))
.subscribe(timelineResponse -> {
getView().onIndexDataLoaded(timelineResponse.getMeta().getArtists(), timelineResponse.getVideos());
int size = timelineResponse.getVideos().size();
lastVideoId = timelineResponse.getVideos().get(size - 1).getId();
}, throwable -> getView().onIndexDataFailed());
compositeDisposable.add(disposable);
}
public void loadMore() {
if (lastVideoId != null && lastVideoId.equals("-1")) {
return;
}
Disposable disposable = videoService
.getTimeline(lastVideoId)
.compose(Util.schedulers())
.doOnSubscribe(__ -> getView().startLoading())
.doFinally(() -> getView().stopLoading())
.doOnError(throwable -> Timber.d(throwable, "failed loading more items"))
.subscribe(timelineResponse -> {
getView().onNextPageLoaded(timelineResponse.getVideos());
int size = timelineResponse.getVideos().size();
if (size == 0) {
lastVideoId = "-1";
} else {
lastVideoId = timelineResponse.getVideos().get(size - 1).getId();
}
}, throwable -> getView().onIndexDataFailed());
compositeDisposable.add(disposable);
}
}
我想知道的是,我每次访问时是否仍然需要执行无效检查getView()
?在拆卸之前处理掉所有一次性用品不就可以解决这个问题吗?