英文:
How to queue the same method call in Kotlin/Java
问题
使用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
是否有方法可以排队方法调用,并在执行另一个方法之前等待执行完成?
英文:
I use DiffUtil to calculate for data changes in RecyclerView. The calculation is done on schedulers thread asynchronously with RxJava like below:
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) }
}
}
When the function is called consecutively, calculation of large dataset will be finished last
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
Is there any way to queue method calls and wait for the execution to finish before executing another one?
答案1
得分: 1
我认为你可以使用 [Scheduler.single()](http://reactivex.io/RxJava/javadoc/io/reactivex/schedulers/Schedulers.html#single--) 来创建一个单线程的 Worker。它将会按顺序处理请求。
fun setResultItems(list: List<FlightResultItem>?) {
Log.d("debugfilter", " setResultItems 的大小:" + (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", " 在 DIFF 之后的 setResultItems 大小:" + (newList.size))
resultItems = ArrayList(newList)
diffResult.dispatchUpdatesTo(this)
}.also { mCompositeSubscription?.add(it) }
}
}
英文:
I think you can use Scheduler.single() to create a Single threaded Worker. It will process the requests Sequentially.
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) }
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论