2017-03-15 147 views
1

之前用户没有加载速度不够快,我打这个按钮火力地堡登录赛格瑞

override func viewDidLoad() { 
    FIRAuth.auth()?.signIn(withEmail: "[email protected]", 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 ...

回答

1

我觉得你的观察者应该改变。我看到这个的tableview将是某种形式的聊天,所以你可能要保持基准活着而用户在该视图控制器等等,而不是使用observeSingleEvent你可以只observe,你离开视图时手动删除观察者。

另一件事是,你正在观察时,你应该观察childAdded仿佛同时被加入任何情况下,值“聊天”查看用户将不会收到它。

更改下面的一行,看看这对你的作品。

ref.child("users").child(currentUser.uid).observe(.childAdded, with: { (snapshot) in 

希望它有帮助!

+0

感谢的人!说得通 –