Android,Kotlin妙用suspendCancellableCoroutine,同步执行异步回调代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android,Kotlin妙用suspendCancellableCoroutine,同步执行异步回调代码相关的知识,希望对你有一定的参考价值。
妙用suspendCancellableCoroutine,同步执行异步回调代码
For Example
interface onRes
fun onSuccess(msg: String)
fun onFailed(e: Exception)
- 原先我们写耗时回调
fun longTimeMethod(result: onRes)
//耗时操作
result.onSuccess(msg)
result.onFailed(Exception("..."))
- 妙用suspendCancellableCoroutine
suspend fun longTimeMethod(): String = suspendCancellableCoroutine
longTimeMethod(object : onRes
override fun onSuccess(msg: String)
if (it.isCancelled) return
it.resume(t)
override fun onFailed(e: Exception)
it.resumeWithException(e)
)
以上是关于Android,Kotlin妙用suspendCancellableCoroutine,同步执行异步回调代码的主要内容,如果未能解决你的问题,请参考以下文章
android Handlerr.removeCallbacksAndMessages(null)的妙用
Android图片加载框架 Universal-Image-Loader 妙用