2016-09-16 20 views
2

按鈕按下我正在從Firebase獲得的陣列上執行某些過濾器。例如其中之一是:在加載時從Firebase數組上運行排序?

self.PersonalSearchesList = PersonalSearchesList.sorted{ $0.users > $1.users } 
     self.tableView.reloadData() 

的問題是,我希望當視圖最初加載我想要被applied..but過濾器之一,我不知道從哪裏跑過濾。

如果我在查詢後追加數組時運行排序,那麼我的信息/單元格將會得到不正確的信息。

我無法在viewDiDLoad中運行它,因爲Firebase的觀察者正在ViewDidLoad中運行,並且此時陣列不會被填充。

編輯:

currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in 
      if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] { 
       for snap in snapshots { 
        DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 
         if snapshot.value != nil { 
          if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> { 
           DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 
            self.PersonalSearchesList.append(eachSearch) 
           }) 
          } 
         } 
        }) 
       } 
      } 
     } 
     initialLoadOver = true 
    } 

那麼在什麼時候是異步過這個?在哪一行的結尾括號之後,我需要做些什麼?

+0

顯示firebase closure的代碼,在這裏你要在數組中添加數據。 –

+0

@NiravD剛剛編輯 – user6820041

回答

2

您可以創建排序列表並在其設定的一個實例,你可以重新載入數據

var mySortedList = [YourListObject](){ 
    didSet{ 
     self.tableView.reloadData() 
    } 
} 

那麼當你的附加操作完成後,只是排序它。

currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in 
      if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] { 
       for snap in snapshots { 
        DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 
         if snapshot.value != nil { 
          if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> { 
           DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 
            self.PersonalSearchesList.append(eachSearch) 
           }) 
          } 
         } 
        }) 
       } 
       self.mySortedList = PersonalSearchesList.sort({ (element1, element2) -> Bool in 
        return element1.users! > element2.users! 
       }) 
      } 
     } 
     initialLoadOver = true 
    } 
+0

這很有道理。我編輯過,以顯示我實際上正在執行附加操作的位置。不確定附加操作完成的位置。 – user6820041

+0

@Hibernia在for循環之後 –