gpt4 book ai didi

android - WorkManager alpha10 ListenableFuture 用法

转载 作者:搜寻专家 更新时间:2023-11-01 09:23:11 25 4
gpt4 key购买 nike

我在尝试实现最新的 WorkManager-alpha10 ListenableFuture 以处理流程完成时遇到一些问题。

现在我的 JobOrganizer 类中有以下内容

private fun enqueueDownloadWork(): ListenableFuture<Void> {
val work = WorkManager.getInstance()
.beginWith(dwdTypologiesJob)
.then(dwdElementsJob)
.then(dwdAnomaliesJob)
return work.enqueue()
}

private fun createDownloadWorkRequests() {
dwdTypologiesJob = OneTimeWorkRequestBuilder<DWDAnomalyTypesJob>()
.addTag("download_typologies_work")
.build()
dwdElementsJob = OneTimeWorkRequestBuilder<DWDElementsJob>()
.addTag("download_elements_work")
.build()
dwdAnomaliesJob = OneTimeWorkRequestBuilder<DWDAnomaliesJob>()
.addTag("download_anomalies_work")
.build()
}

fun downloadData(): ListenableFuture<Void> {
createDownloadWorkRequests()
return enqueueDownloadWork()
}

这是我的电话,谁应该听取完成事件。

val listenable = JobOrganizer.downloadData()
listenable.addListener({
Log.d("Listenable", "Did something 1");
}, {
Log.d("Listenable", "Did something 2");
})

我仍然不知道 Runnable 和 Executor 如何处理这个函数。谁能解释一下?

谢谢

最佳答案

你需要同时实例化Runnable和Executor,比如当你想在当前线程直接执行Runnable时:

.addListener(
object:Runnable {
override fun run() {
Log.d("Listenable", "Did something 1");
}
},
object:Executor {
override fun execute(command: Runnable?) {
command?.run()
}
}

您可以在 https://developer.android.com/reference/java/util/concurrent/Executor 上找到有关执行器的更多示例

关于android - WorkManager alpha10 ListenableFuture 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52876776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com