Android系统。我是否需要在onBindViewHolder中处理?

时间:2017-05-15 06:32:19

标签: android realm rx-java2

我正在我的适配器中的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吗?如果是这样,那么生命周期中的时刻最好是这样做吗?

1 个答案:

答案 0 :(得分:2)

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

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