Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用协同程序替换自定义回调接口?_Android_Kotlin Coroutines - Fatal编程技术网

Android 用协同程序替换自定义回调接口?

Android 用协同程序替换自定义回调接口?,android,kotlin-coroutines,Android,Kotlin Coroutines,安卓工作室3.6 我的自定义回调接口: interface RecoveryPasswordConfirmCodeCallback { fun onSuccess() fun onError(ex: Throwable?) } 使用: val result=TransportService.recoverPasswordConfirmCode( 确认码, 前任, 对象:RecoveryPasswordConfirmCodeCallback{ 覆盖成功的乐趣(){ } 覆盖有趣的

安卓工作室3.6

我的自定义回调接口:

interface RecoveryPasswordConfirmCodeCallback {
    fun onSuccess()
    fun onError(ex: Throwable?)
}
使用:

val result=TransportService.recoverPasswordConfirmCode(
确认码,
前任,
对象:RecoveryPasswordConfirmCodeCallback{
覆盖成功的乐趣(){
}
覆盖有趣的错误(例如:可丢弃?){
如果(ex为InvalidOtpException){
toastMessage.value=SingleEvent(
getApplication().applicationContext.getString(
R.string.不正确的\u确认\u代码
)
)
}否则{
toastMessage.value=SingleEvent(
getApplication().applicationContext.getString(
R.string.default\u错误消息
))
}
}
})
有趣的recoverPasswordConfirmCode(
确认码:字符串,
例:NeedTfaException,
回调:RecoveryPasswordConfirmCodeCallback
) {
//这里有一些代码
}

很好。很好。但是是否可以用Kotlin的协同程序替换我的自定义回调接口。我不想只为execute方法创建自定义接口
recoverPasswordConfirmCode

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

// 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 */)

}
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)
    }
}

请注意,您必须在协同路由上下文中调用suspend函数

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

// 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 */)

}
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方法