首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否将自定义回调接口替换为协程?

问是否将自定义回调接口替换为协程?
EN

Stack Overflow用户
提问于 2020-03-10 21:19:55
回答 2查看 291关注 0票数 0

Android Studio 3.6

我的自定义回调接口:

代码语言:javascript
复制
interface RecoveryPasswordConfirmCodeCallback {
    fun onSuccess()
    fun onError(ex: Throwable?)
}

使用:

代码语言:javascript
复制
val result = TransportService.recoverPasswordConfirmCode(
                    confirmCode,
                    ex,
                    object : RecoveryPasswordConfirmCodeCallback {
                        override fun onSuccess() {

                        }

                        override fun onError(ex: Throwable?) {

                            if (ex is InvalidOtpException) {
                                toastMessage.value = SingleEvent(
                                    getApplication<Application>().applicationContext.getString(
                                        R.string.incorrect_confirm_code
                                    )
                                )
                            } else {
                                toastMessage.value = SingleEvent(
                                    getApplication<Application>().applicationContext.getString(
                                        R.string.default_error_message
                                    ))
                            }
                        }
                    })


 fun recoverPasswordConfirmCode(
            confirmCode: String,
            ex: NeedTfaException,
            callBack: RecoveryPasswordConfirmCodeCallback
        ) {
           //some code here
        }

好的。它工作得很好。但是..。有没有可能用Kotlin的协程替换我的自定义回调接口。我不想只为execute方法recoverPasswordConfirmCode创建自定义接口

EN

回答 2

Stack Overflow用户

发布于 2020-03-10 22:11:48

您可以将recoverPasswordConfirmCode()转换为挂起函数,并以密封类的形式返回结果,以指示它是错误还是有效响应。如下所示:

代码语言:javascript
复制
// Generic response class
sealed class Response<out T>{
    data class Error(val ex: Throwable) : Response<Nothing>()
    data class Data<T>(val data: T) : Response<T>()
}

// in your TransportService class
suspend fun recoverPasswordConfirmCode(confirmCode, ex): Response<RecoverPasswordResponse>{
    // Do your stuff here 
    // return Response.Data<RecoverPasswordResponse>(/* your data object here */)

}

然后像这样调用它并检查响应类型:

代码语言:javascript
复制
val result = TransportService.recoverPasswordConfirmCode(confirmCode, ex)
when(result){
    is Response.Error -> // Do something
    is Response.Data -> // Do something
}

请注意,您必须在协程上下文中调用suspend函数。

票数 1
EN

Stack Overflow用户

发布于 2020-03-11 05:41:52

您不需要创建自定义接口。像这样使用你的API:

代码语言:javascript
复制
suspend fun recoverPasswordConfirmCode(confirmCode: String): YourReturnType = suspendCancellableCoroutine { cont ->
    try {
        val result = //Do your blocking API calls here
        if(result.code == confirmCode) //Check confirm code is correct
            cont.resume(YourResult) //Return your result here
        else
            cont.resumeWithException(YourException) //Throw an exception otherwise
    } catch (e: Exception) {
        cont.resumeWithException(e)
    }
}

在协程作用域内调用recoverPasswordConfirmCode方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60618507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档