我正在点击这个按钮
override func viewDidLoad() {
FIRAuth.auth()?.signIn(withEmail: "hakuna.matata.kitty@gmail.com", password: "password") {
(user, error) in
// TODO: SET UP A SPINNER ICON
print("Logged into hmk")
self.performSegue(withIdentifier: "login", sender: self)
}
}
如您所见,在回调时它重定向到 UITableViewController。填充器代码位于 UITableViewController 的初始化程序中,闭包中的唯一目的是填充 self.chatsList
有时,每当我测试应用程序时,UITableViewController 都是空白的。
ref.child("users").child(currentUser.uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard snapshot.exists() else {
print("Error: Path not found")
return
}
let value = snapshot.value as? NSDictionary
// Convert a dict of dicts into an array of dicts
// You will lose chatID in the process, so be warned
// Unless you would like to to keep the data in a struct
for (chatIdKey, secondDict) in value?["chats"] as! [String: NSDictionary] {
// This only appends metadata for the last chat
// Does not load every chat message
self.chatsList.append(Chat(chatId: chatIdKey, targetChat: secondDict))
}
})
但是当我按下后退键,等待足够长的时间并再次登录时,表格已正确填充。
如何确保它每次都能正确加载?我希望回调上的 performSegue 能保证我们确实有一个 currentUser...
最佳答案
我认为你的观察者应该改变。我看到这个 tableview 将是某种聊天,所以您可能希望在用户位于该 ViewController 中时保持引用有效,因此您可以 observe
而不是使用 observeSingleEvent
和您在离开 View 时手动移除观察者。
另一件事是,当您可能应该观察 childAdded 时,您正在观察该值,就好像在“聊天” View 中添加了任何事件一样,用户不会收到它。
更改下面的行,看看这是否适合您。
ref.child("users").child(currentUser.uid).observe(.childAdded, with: { (snapshot) in
希望对您有所帮助!
关于ios - Firebase 登录用户在 segue 之前加载速度不够快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42820838/