我正在我的适配器中的MayBe
observable中执行一个繁重的请求。
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final RealmArticle obj = getItem(position);
int idTask = obj.getIdTask();
Disposable mayBeCount = Maybe.fromCallable(()->
{
Realm bgInstance = Realm.getInstance(Realm.getDefaultInstance().getConfiguration());
return bgInstance.where(RealmArticle.class).findAll().where().equalTo("idTask", idTask).equalTo("completed", true).count()
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(res->{
holder.row_count.setText(res);
this.notifyItemChanged(position);
},
throwable -> Log.e(TAG, String.format("%s, %s", "Can not get items count", throwable.getMessage())));
}
问题是:我应该处置mayBeCount
吗?如果是这样,那么生命周期中的时刻最好是这样做吗?
答案 0 :(得分:2)
您可以使用onViewDetachedFromWindow
取消操作。因此,我会将Disposable
作为属性添加到您的查看者。
@Override
public void onViewDetachedFromWindow(MyViewHolder holder) {
holder.mayBeCount.dispose();
}