当我第二次调用功能时,Kotlin流量什么都没有发出
我尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚测试了代码,它对我有用。我调用了
onDeleteAccountClicked()
三次,调用之间存在延迟,并且打印了emit
lambda 内的所有三个“发出日志”日志。尝试删除在emit
lambda 中调用firebaseAuth.deleteUser()
并进行测试。当用户已被删除时调用FirebaseUser.delete
函数会抛出FirebaseAuthInvalidUserException
异常。也许这就是您没有看到日志的原因 - 因为 FirebaseUser.delete 函数抛出异常。我认为用于仅调用一个函数的结构有点复杂,我建议摆脱
_deleteAccount
流程并将firebaseAuth.deleteUser()
包装在内>try-catch
(您甚至不需要为此启动协程):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" insideemit
lambda were printed. Try to remove callingfirebaseAuth.deleteUser()
insideemit
lambda and test. CallingFirebaseUser.delete
function when user is already deleted throwsFirebaseAuthInvalidUserException
exception. Maybe that's why you didn't see logs - becauseFirebaseUser.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 wrapfirebaseAuth.deleteUser()
insidetry-catch
(you even don't need to launch a coroutine for that):