2017-03-15 138 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

感謝的人!說得通 –