ios - 每次移动到不同 View Controller 时都会加载 Firestore 数据

标签 ios swift xcode firebase google-cloud-firestore

func loadUserThings() { 


        Firestore.firestore().collection("users").document((Auth.auth().currentUser?.uid)!).getDocument { (snapshot, error) in
            if let document = snapshot {
                var data = document.data()
                self.nameLabel.text = data?["username"] as? String ?? ""
                self.usernameLabel.text = data?["username"] as? String ?? ""
            }
    }

}

上面的函数是在 View Controller 的viewdidLoad()中调用的。

我正在从 firebase firestore 检索数据。当我在 View Controller 之间移动或打开应用程序时,标签会再次加载。数据正在缓存,正如我在没有互联网时可以看到的那样,但即使如此,加载也需要时间。

每次在标签中加载Firestore数据

enter image description here

最佳答案

当您使用 Firestone 的 getData 函数时,就会发生这种情况。 使用快照监听器将消除此问题。

关于ios - 每次移动到不同 View Controller 时都会加载 Firestore 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807100/

相关文章:

ios - 更改 UIViewController 的方向会影响另一个 UITableViewController 的边界

ios - 单击不同按钮时删除按钮上的选定状态(Swift)

ios - Apple Mach-O 链接器警告。 lib/libxyz.a 在文件 lib/libxyz.a 中缺少所需的体系结构 i386

ios - 是否可以直接在 XCode 8 中创建 .xcarchive?

iphone - 如何安全地升级/备份使用核心数据的现有应用程序?

ios - App Store不接受Ant Media Server的iOS框架吗?

ios - 某些设备上的 `dispatch_semaphore_wait()` 有问题吗?

ios - 如何在 UIVIew 的底部添加阴影

ios - 将来自不同 TableViewController 的 4 个单元与单个 ViewController 链接

swift - 如何使用 didSet 更改 UITextField (IBOutlet) 的文本?