在Anko coroutines kotlin中推迟了什么?

时间:2017-06-28 05:59:12

标签: android kotlin anko kotlin-coroutines

在kotlin的Anko coroutines库中,有一个功能 bg(),可以在后台线程上轻松执行你的代码。在该返回类型中延迟。那么什么是延期

参与链接

(1) https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Deferred.kt

(2) https://github.com/Kotlin/anko/wiki/Anko-Coroutines#bg

  fun getData(): Data { ... }
  fun showData(data: Data) { ... }

  async(UI) {
      val data: Deferred<Data> = bg {
      // Runs in background
      getData()
      }

      // This code is executed on the UI thread
      showData(data.await())
  }

1 个答案:

答案 0 :(得分:6)

如果您原谅我,请从问题的第一个链接引用Deferred类文档中的第一句话开始:

  

延迟价值是一个无阻碍可取消的未来。

事实上,deferred是 future promise see this wikipedia article)的同义词。

Deferred类是kotlinx-coroutines项目的一部分,为Kotlin协同程序提供库支持。开始学习更多内容的推荐方法是阅读guide