Android 如何通过SuspendCancelableCorroutine防止使用try-catch
我做了一个协同路由,在我的交互程序中调用一个异步方法,问题是当继续块被执行时,我不想使用try-catch块,因为我认为这不是正确的方法 节目主持人 签署人 问题 有没有其他方法可以从我的交互者那里捕获继续简历和演示者中的异常 我已经读到,try-catch块是一种反模式,用于协同路由 如果异步块可能引发异常,请不要依赖于用 试着/抓住挡块Android 如何通过SuspendCancelableCorroutine防止使用try-catch,android,kotlin,coroutine,kotlin-coroutines,android-mvp,Android,Kotlin,Coroutine,Kotlin Coroutines,Android Mvp,我做了一个协同路由,在我的交互程序中调用一个异步方法,问题是当继续块被执行时,我不想使用try-catch块,因为我认为这不是正确的方法 节目主持人 签署人 问题 有没有其他方法可以从我的交互者那里捕获继续简历和演示者中的异常 我已经读到,try-catch块是一种反模式,用于协同路由 如果异步块可能引发异常,请不要依赖于用 试着/抓住挡块 您没有使用async块,所以try-catch正是您应该用来包装suspend方法调用的异常的方法。谢谢ian,如果我的SignInWithCorouti
您没有使用
async
块,所以try-catch正是您应该用来包装suspend
方法调用的异常的方法。谢谢ian,如果我的SignInWithCoroutineTest中还有一个asyncmethod,例如,在if(it.issuceFull)之后,我是否应该在我的交互程序中添加try-catch以等待其他方法?这样做可能与回调hells相同,谢谢如果您始终使用suspend
方法,那么异常的处理就像它们是阻塞方法一样-您可以在任何适当的级别捕获它们。
override fun signInWithCoroutines(email: String, password: String) {
launch {
view?.showProgress()
try{
signInInteractor.signInWithCoroutinesTest(email,password)
}catch(e FirebaseLoginException){ ---> want to know if there is another way to do this
view.showError(e.getMessage())
}
view?.hideProgress()
view?.navigateToMain()
}
}
override suspend fun signInWithCoroutinesTest(email: String, password: String): Unit = suspendCancellableCoroutine { continuation ->
FirebaseAuth.getInstance()?.signInWithEmailAndPassword(email, password)?.addOnCompleteListener {
if (it.isSuccessful) {
continuation.resume(Unit)
} else {
continuation.resumeWithException(FirebaseLoginException("Error while login in, try again")
}
}
}