相当于 Kotlin 协程中的 observeOn 和 subscribeOn

马赫迪-马尔夫

例如:

Observable.fromCallable<Int> {
   backgroundTask() // returns an integer
   }
   .observeOn(AndroidSchedulers.mainThread())
   .subscribeOn(Schedulers.io())
   .subscribe ({ number -> /* success */ }, { error -> /* fail */ })

通常在后台(另一个线程)执行任务并将其结果返回到主线程中。

此代码片段将如何使用Kotlin 协程

乔姆

您可以使用withContext(). 例如,

launch(Dispatchers.MAIN) {
    //main thread here
    val result = withContext(Dispatchers.IO) {
        //IO thread here
        backgroundTask()
    }
    //main thread here again
    //doing something with result
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章