如何在 Kotlin/Java 中排队相同的方法调用

huangapple go评论71阅读模式
英文:

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&lt;FlightResultItem&gt;?) {
    Log.d(&quot;debugfilter&quot;, &quot;            setResultItems of size: &quot; + (list?.size?: 0))
    list?.let { newList -&gt;
        Single.fromCallable { DiffUtil.calculateDiff(ResultItemDiffUtilCallback(ArrayList(newList), ArrayList(resultItems))) }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { diffResult -&gt;
                    Log.d(&quot;debugfilter&quot;, &quot;            setResultItems AFTER DIFF of size: &quot; + (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&lt;FlightResultItem&gt;?) {
Log.d(&quot;debugfilter&quot;, &quot;            setResultItems of size: &quot; + (list?.size?: 0))
list?.let { newList -&gt;
    Single.fromCallable { DiffUtil.calculateDiff(ResultItemDiffUtilCallback(ArrayList(newList), ArrayList(resultItems))) }
        .subscribeOn(Schedulers.single())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { diffResult -&gt;
            Log.d(&quot;debugfilter&quot;, &quot;            setResultItems AFTER DIFF of size: &quot; + (newList.size))
            resultItems = ArrayList(newList)
            diffResult.dispatchUpdatesTo(this)

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

huangapple
  • 本文由 发表于 2020年9月3日 12:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63717053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定