转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/122058124
本文出自【赵彦军的博客】
suspendCancellableCoroutine
普通的回调函数:
interface Result<T> {
fun onSuccess(t: T)
fun onFailed(e: Exception)
}
回调方法,模拟耗时操作
fun longTimeMethod(result: Result<String>) {
thread {
Thread.sleep(5000)
if (System.currentTimeMillis() % 10 > 6) {
result.onSuccess("${System.currentTimeMillis()}")
} else {
result.onFailed(Exception("FAILED"))
}
}
}
去掉回调,转换为挂起函数:
suspend fun getResult(): String =
suspendCancellableCoroutine {
longTimeMethod(object : Result<String> {
override fun onSuccess(t: String) {
if (it.isCancelled) return
it.resume(t)
}
override fun onFailed(e: Exception) {
it.resumeWithException(e)
}
})
}
使用:
GlobalScope.launch {
try {
val result = getResult()
} catch (exception: Exception) {
}
}
源码分析:
我们看一下 it.resume()
、it.resumeWithException()
方法,其实是调用 resumeWith(result: Result)
suspendCancellableCoroutine 和 suspendCoroutine 区别
- 使用 suspendCancellableCoroutine 和 suspendCoroutine 都可以将回调函数转换为协程
- SuspendCancellableCoroutine 返回一个 CancellableContinuation, 它可以用 resume、resumeWithException 来处理回调 和抛出 CancellationException 异常。它与 suspendCoroutine的唯一区别就是 SuspendCancellableCoroutine 可以通过 cancel() 方法手动取消协程的执行,而 suspendCoroutine 没有该方法。
- 尽可能使用 suspendCancellableCoroutine 而不是 suspendCoroutine ,因为协程的取消是可控的
举个例子:使用网络请求数据时,如果请求时间过长,用户可以手动取消掉协程的执行。这时会抛出一个 CancellationException 异常,但是将该异常try{}catch{}捕获后就不会影响后续代码的执行。而使用 suspendCoroutine 只能干等着被 resume 或者 resumeWithException ,因为它没有该功能。