0

我使用 DiffUtil 计算 RecyclerView 中的数据变化。计算是在调度程序线程上使用 RxJava 异步完成的,如下所示:

fun setResultItems(list: List<FlightResultItem>?) {
    Log.d("debugfilter", "            setResultItems of size: " + (list?.size?: 0))
    list?.let { newList ->
        Single.fromCallable { DiffUtil.calculateDiff(ResultItemDiffUtilCallback(ArrayList(newList), ArrayList(resultItems))) }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { diffResult ->
                    Log.d("debugfilter", "            setResultItems AFTER DIFF of size: " + (newList.size))
                    resultItems = ArrayList(newList)
                    diffResult.dispatchUpdatesTo(this)

                }.also { mCompositeSubscription?.add(it) }
    }
}

连续调用该函数时,大数据集的计算将最后完成

D: debugfilter : setResultItems of size: 113 
D: debugfilter : setResultItems of size: 7 
D: debugfilter : setResultItems AFTER DIFF of size: 7 
D: debugfilter : setResultItems AFTER DIFF of size: 113

有没有办法将方法调用排队并等待执行完成,然后再执行另一个?

4

1 回答 1

1

我认为您可以使用Scheduler.single()创建单线程 Worker。它将按顺序处理请求。

fun setResultItems(list: List<FlightResultItem>?) {
Log.d("debugfilter", "            setResultItems of size: " + (list?.size?: 0))
list?.let { newList ->
    Single.fromCallable { DiffUtil.calculateDiff(ResultItemDiffUtilCallback(ArrayList(newList), ArrayList(resultItems))) }
        .subscribeOn(Schedulers.single())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { diffResult ->
            Log.d("debugfilter", "            setResultItems AFTER DIFF of size: " + (newList.size))
            resultItems = ArrayList(newList)
            diffResult.dispatchUpdatesTo(this)

        }.also { mCompositeSubscription?.add(it) }
}
}
于 2020-09-03T05:42:43.267 回答