【问题标题】:SwiftUI firebase - change user's displayName - how to refresh view instantlySwiftUI firebase - 更改用户的 displayName - 如何立即刷新视图
【发布时间】:2020-05-31 13:27:15
【问题描述】:

我使用 swiftui 创建了一个基本的 firebase 登录应用程序(使用本教程:https://www.youtube.com/watch?v=FhLEwqyVSjE)。一切正常。

在我的 ContentView.swift 中,我添加了一个 TextField 来更改用户的显示名称。为此,我使用以下逻辑在 SessionStore.swift 中添加了一个 changeDisplayName 函数:

let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = displayName
changeRequest?.commitChanges { (error) in
  // ...
}

函数由我的 ContentView 中的按钮调用。更改 displayName 也可以,但我的 ContentView 仍然保留旧的 displayName 直到我重新启动应用程序,因为只有这样 ContentView 才会再次调用 session.listen() 并从 SessionStore 获取新的 displayName。

为了解决这个问题,每当我的 ContentView 调用 SessionStore 中的 changeDisplayName 函数并且不返回错误时,我都会在 ContentView 中调用 session.unbind()session.listen()。这很有效,每次更改时都会使用新的 displayName 重新加载我的 ContentView。

这是一种好的做法还是我错过了解决这个问题的更简洁的方法?

【问题讨论】:

    标签: firebase firebase-authentication swiftui


    【解决方案1】:

    也许尝试使用Auth.auth().currentUser?.reload()。这可能不起作用,因为我过去在使用 isEmailVerified 时遇到过问题,但我想值得一试!

    【讨论】:

    • 那么我认为你的解决方案是最好的。它可以很好地重新加载用户,但是我用来监听已验证电子邮件状态的 authenticationStateListener 没有更新。
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2021-07-08
    • 2022-12-07
    相关资源
    最近更新 更多