【发布时间】:2021-06-25 01:07:00
【问题描述】:
我有一个 KMM 应用,并且有代码:
fun getWeather(callback: (WeatherInfo) -> Unit) {
println("Start loading")
GlobalScope.launch(ApplicationDispatcher) {
while (true) {
val response = httpClient.get<String>(API_URL) {
url.parameters.apply {
set("q", "Moscow")
set("units", "metric")
set("appid", weatherApiKey())
}
println(url.build())
}
val result = Json {
ignoreUnknownKeys = true
}.decodeFromString<WeatherApiResponse>(response).main
callback(result)
// because ApplicationDispatcher on IOS do not support delay
withContext(Dispatchers.Default) { delay(DELAY_TIME) }
}
}
}
如果我将withContext(Dispatchers.Default) { delay(DELAY_TIME) } 替换为delay(DELAY_TIME) 执行将永远不会返回到while 循环,它只会进行一次迭代。
而 IOS 的 ApplicationDispatcher 看起来像:
internal actual val ApplicationDispatcher: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue())
internal class NsQueueDispatcher(
private val dispatchQueue: dispatch_queue_t
) : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatchQueue) {
block.run()
}
}
}
从delay 源代码我可以猜到,DefaultDelay 应该被返回并且应该有类似的行为有/没有withContext(Dispatchers.Default)
/** Returns [Delay] implementation of the given context */
internal val CoroutineContext.delay: Delay get() = get(ContinuationInterceptor) as? Delay ?: DefaultDelay
谢谢!
附:我从ktor-samples 得到ApplicationDispatcher。
【问题讨论】:
标签: ios kotlin kotlin-coroutines kotlin-multiplatform kmm