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?