当我第二次调用功能时,Kotlin流量什么都没有发出

发布于 2025-01-18 19:47:20 字数 1201 浏览 2 评论 0原文

我尝试使用Kotlin Flow(共享流)在Firebaseauth中实现删除用户。 在OnDeleteAccountClicked()中,有delete()方法从firebaseauth调用,它可能会抛出AuthreAuthenticationRequiredException。当抛出异常时,应用程序将重定向到另一个片段以重新验证,然后再次调用ondeleteaccountclicked(),但流量没有发出。

viewModel

    private val _deleteAccount = MutableSharedFlow<() -> Unit>()

    fun onDeleteAccountClicked() {
        logd("outside the viewModelScope")
        viewModelScope.launch {
            logd("inside the viewModelScope")
            _deleteAccount.emit {
                logd("emitting log")
                firebaseAuth.deleteUser()
                //throw AuthReauthenticationRequiredException()
            }
        }
    }

    init {
        viewModelScope.launch {
            _deleteAccount
                .onEach {
                    it()
                }
                .catch {
                    if (it is AuthReauthenticationRequiredException) {
                        _redirectToSignInMethodsScreen.emit(Unit)
                    }
                }
                .collect()
        }
    }

logs“在ViewModelScope之外”和“在ViewModelScope内部”显示,每次调用该方法时都显示,但仅是第一次“发射log”。

我什至想以正确的方式做吗?

I try to implement deleting user in FirebaseAuth using Kotlin flow (SharedFlow).
In onDeleteAccountClicked() there is delete() method called from FirebaseAuth which may throw AuthReauthenticationRequiredException. When the exception is thrown, app redirects to another fragment to reauthenticate, then call onDeleteAccountClicked() once again, but flow emits nothing.

ViewModel

    private val _deleteAccount = MutableSharedFlow<() -> Unit>()

    fun onDeleteAccountClicked() {
        logd("outside the viewModelScope")
        viewModelScope.launch {
            logd("inside the viewModelScope")
            _deleteAccount.emit {
                logd("emitting log")
                firebaseAuth.deleteUser()
                //throw AuthReauthenticationRequiredException()
            }
        }
    }

    init {
        viewModelScope.launch {
            _deleteAccount
                .onEach {
                    it()
                }
                .catch {
                    if (it is AuthReauthenticationRequiredException) {
                        _redirectToSignInMethodsScreen.emit(Unit)
                    }
                }
                .collect()
        }
    }

Logs "outside the viewModelScope" and "inside the viewModelScope" shows every time when the method is called, but "emitting log" only for the first time.

Am I even trying to do it the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2025-01-25 19:47:20

我刚刚测试了代码,它对我有用。我调用了 onDeleteAccountClicked() 三次,调用之间存在延迟,并且打印了 emit lambda 内的所有三个“发出日志”日志。尝试删除在 emit lambda 中调用 firebaseAuth.deleteUser() 并进行测试。当用户已被删除时调用 FirebaseUser.delete 函数会抛出 FirebaseAuthInvalidUserException 异常。也许这就是您没有看到日志的原因 - 因为 FirebaseUser.delete 函数抛出异常。

我认为用于仅调用一个函数的结构有点复杂,我建议摆脱 _deleteAccount 流程并将 firebaseAuth.deleteUser() 包装在 内>try-catch (您甚至不需要为此启动协程):

fun onDeleteAccountClicked() {
    try {
        firebaseAuth.deleteUser()
    } catch(e: AuthReauthenticationRequiredException) {
        _redirectToSignInMethodsScreen.emit(Unit)
    }
}

I just tested the code, and it works for me. I called onDeleteAccountClicked() three times with delay between calling, and all three logs "emitting log" inside emit lambda were printed. Try to remove calling firebaseAuth.deleteUser() inside emit lambda and test. Calling FirebaseUser.delete function when user is already deleted throws FirebaseAuthInvalidUserException exception. Maybe that's why you didn't see logs - because FirebaseUser.delete function throws an exception.

I think the structure you use for calling just one function is a bit complicated, I can suggest to get rid of _deleteAccount flow and just wrap firebaseAuth.deleteUser() inside try-catch (you even don't need to launch a coroutine for that):

fun onDeleteAccountClicked() {
    try {
        firebaseAuth.deleteUser()
    } catch(e: AuthReauthenticationRequiredException) {
        _redirectToSignInMethodsScreen.emit(Unit)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文