Android。我需要在 onBindViewHolder 中处理 Maybe 吗?

Android. Do I need to dispose Maybe inside onBindViewHolder?

我正在 MayBe 内部执行一个繁重的请求,在我的适配器中可以观察到。

@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 吗?如果是这样,在生命周期的哪个时刻做这件事比较好?

您可以使用onViewDetachedFromWindow取消操作,希望如此。因此,我会将 Disposable 作为属性添加到您的查看器中。

@Override
public void onViewDetachedFromWindow(MyViewHolder holder) {
    holder.mayBeCount.dispose();
}