2017-12-27 154 views
0

我試圖從firebase中獲取數據並傳遞給tableview。在正確的地方調用reloadData()

// Model 
      import UIKit 
      import Firebase 

       struct ProfInfo { 

        var key: String 
        var url: String 
        var name: String 

        init(snapshot:DataSnapshot) { 

         key = snapshot.key 
         url = (snapshot.value as! NSDictionary)["profileUrl"] as? String ?? "" 
         name = (snapshot.value as! NSDictionary)["tweetName"] as? String ?? "" 
       } 
      } 

    // fetch 
      var profInfo = [ProfInfo]() 

      func fetchUid(){ 
        guard let uid = Auth.auth().currentUser?.uid else{ return } 
        ref.child("following").child(uid).observe(.value, with: { (snapshot) in 
         guard let snap = snapshot.value as? [String:Any] else { return } 
         snap.forEach({ (key,_) in 
          self.fetchProf(key: key) 
         }) 
        }, withCancel: nil) 
       } 

       func fetchProf(key: String){ 
        var outcome = [ProfInfo]() 
         ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in 
           let info = ProfInfo(snapshot: snapshot) 
           outcome.append(info) 
          self.profInfo = outcome 
          self.tableView.reloadData() 
         }, withCancel: nil) 
       } 

    //tableview 
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
        return profInfo.count 
       } 

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

        let cell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as! FollowingTableViewCell 

        cell.configCell(profInfo: profInfo[indexPath.row]) 

        return cell 
       } 

但是它返回一行,但profInfo實際上有兩行。當我在fetchProf裏執行print(self.profInfo)時,它返回兩個值。但傳遞給tableview後,它成爲一個。我不知道,但我想是因爲我把reloadData()放在錯誤的地方,因爲我打中斷點和reloadData()調用兩次。所以,我認爲profInfo取代新的價值。我在不同的地方打電話,但沒有工作。我對麼?如果是這樣,我應該在哪裏撥打reloadData()?如果我錯了,我該如何解決這個問題?先謝謝你!

+0

當reloadData被調用兩次時,print(self.profInfo)在每種情況下都會打印什麼? –

回答

1

您需要將新數據附加到profinfo陣列。僅僅用這個代替fetchProf方法: -

func fetchProf(key: String){  
     var outcome = [ProfInfo]()   
     ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in  
     let info = ProfInfo(snapshot: snapshot)   
     outcome.append(info)  
     self.profInfo.append(contentOf: outcome)  
     Dispatch.main.async{ 
     self.tableView.reloadData()  
     } 
    } , withCancel: nil) 
} 
+0

非常感謝你完美的工作! – Daibaku

1

self.tableView.reloadData()必須從主隊列中調用。嘗試

DispatchQueue.main.async { 
    self.tableView.reloadData() 
} 
1

如果您發現下面的函數一件事,你會看到

func fetchProf(key: String){ 
        var outcome = [ProfInfo]() 
         ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in 
           let info = ProfInfo(snapshot: snapshot) 
           outcome.append(info) 

//Here 
/You are replacing value in self.profInfo 
//for the first time when this is called it results In First profile info 
//When you reload here first Profile will be shown 
//Second time when it is called you again here replaced self.profInfo 
//with second Outcome i.e TableView reloads and output shown is only second Profile 
//you had initialised a Array self.profInfo = [ProfInfo]() 
//But you just replacing array with Single value Actually you need to append data 
// I think here is main issue 

self.profInfo = outcome 


//So try Appending data as 
//self.profInfo.append(outcome) instead of self.profInfo = outcome 
//Then reload TableView to get both outputs 
self.tableView.reloadData() 

    }, withCancel: nil) 
} 
1

顯示,因爲當表視圖重載那麼個人資料信息不合並一個內容表視圖所有數據。合併所有數據後,您需要重新加載表格視圖。這會幫助你。

// fetch 
     var profInfo = [ProfInfo]() 

     func fetchUid(){ 
       guard let uid = Auth.auth().currentUser?.uid else{ return } 
       ref.child("following").child(uid).observe(.value, with: { (snapshot) in 
        guard let snap = snapshot.value as? [String:Any] else { return } 
        snap.forEach({ (key,_) in 
         self.fetchProf(key: key) 
        }) 
        // When all key fetched completed the just reload the table view in the Main queue 
        Dispatch.main.async{ 
         self.tableView.reloadData()  
        } 

       }, withCancel: nil) 
      } 

      func fetchProf(key: String){ 
        ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in 
         let info = ProfInfo(snapshot: snapshot) 
         self.profInfo.append(info) // Here just add the outcome object to profileinfo 
        }, withCancel: nil) 
      } 

這種方式不需要處理另一個數組。