CoroutineContext获取调试信息

时间:2019-06-25 10:37:38

标签: kotlin kotlin-coroutines

关于Kotlin协程,我面临的问题较小:

我有一个条目列表,所有条目都可以传递一个“作业”,该作业使用CoroutineContext.IO进行调用。我在滚动列表时注意到一些问题,这些列表一出现就被请求。每个条目调用一个“请求者”,该请求者调用传入的请求:

override fun <T : Any> enqueue(request: () -> Result<T>) {
    job = CoroutineScope(provider.io()).launch {
        (coroutineName)
        try {
            when (val result = request()) {
                is Result.Success -> {
                    notifyAdapter(result)
                }
                is Result.Error -> {
                    notifyAdapter(result)
                }
            }
        } catch (firewall: Exception) {
            logger.e(tag, "Failed to Request Data", firewall)

        }
    }

}

我注意到,这些请求在呈现UI时似乎导致“垃圾”。根据显示的条目,以上代码将在4-12个请求之间执行。 我读到IO CoroutineContext是未绑定的,并且会在需要时在后台创建线程,因此应该没有问题,因为它们都位于不同的线程上。提出请求后,我将请求结果传递回主线程上的调用方:

private suspend fun notifyAdapter(result: Result<Any>) =
    withContext(provider.main()) {
        notifyResult.requestUpdate(id, result)
    }

我仍在尝试更好地理解“棘手”问题,因此是否/何时在IO CoroutineContext上创建线程有某种调试信息?

感谢您的帮助。

0 个答案:

没有答案
相关问题