【发布时间】:2019-09-28 04:45:39
【问题描述】:
我有一个代码,我使用 kotlin couroutines 懒惰地更新 UI。
当我将一些代码放入GlobalScope.async 时,只执行前几行,其余代码不执行
class MyFragment : Fragment(), CoroutineScope {
private lateinit var masterJob: Job
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + masterJob
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
GlobalScope.async(coroutineContext) {
api.fetchOrders()
configureOrders(view!!)
// Some code here ...
}
}
例如在上面的代码中,注释后面的代码没有被执行。如果我将注释上面的代码与上面的代码交换,那么它就不会被执行。
我错过了什么?
【问题讨论】:
-
您没有等待从
async返回的Deffered。我不确定,但它可能会在onViewCreated结束后被取消。另外,您检查过未处理的异常吗?