我制作了一个调用程序,在我的交互器中调用异步方法,问题是当执行延续块时,我不想使用try catch块,因为我认为这不是正确的方法。 / p>
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")
}
}
}
在我的演示者中,还有其他方法可以从我的交互器中捕获继续简历和异常吗?
我已经读到try catch块是与协程一起使用的一种反模式
https://proandroiddev.com/kotlin-coroutines-patterns-anti-patterns-f9d12984c68e
如果异步块可能引发异常,请不要将其包装为 尝试/捕获块。
答案 0 :(得分:1)
您没有使用async
块,因此try catch正是包装suspend
方法调用的异常时应使用的内容。