Solution 1 :
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) }
}
}
Problem :
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?
Comments
Comment posted by Brandon
Could using Schedulers.trampoline() work instead of IO?