如何防止将try catch与suspendCancellableCoroutine一起使用

时间:2019-05-24 22:25:21

标签: android kotlin coroutine kotlin-coroutines android-mvp

我制作了一个调用程序,在我的交互器中调用异步方法,问题是当执行延续块时,我不想使用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()
        }

    }

SignInInteractor

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

  

如果异步块可能引发异常,请不要将其包装为   尝试/捕获块。

1 个答案:

答案 0 :(得分:1)

您没有使用async块,因此try catch正是包装suspend方法调用的异常时应使用的内容。