2016-12-06 44 views
0

我目前正試圖返回所有在我的表格視圖中有3個屬性等於「藝術」,「音樂」&「體育」的用戶。具有3個不等於這3個字符串的屬性的其他用戶將不會顯示在我的表格視圖中。我如何實現這一目標?目前,我只能檢查ONE屬性是否等於ONE字符串就像我在下面的示例中。我有我的數據庫樹的圖像,以及我目前的代碼。任何感謝任何幫助。謝謝!如何檢查Firebase數據庫中的3個或更多值是否相同?

enter image description here

class User: NSObject { 

    var name: String? 
    var email: String? 
    var numberId: String? 

    var attribute1: String? 
    var attribute2: String? 
    var attribute3: String? 

    var password: String? 
    var profileImageUrl: String? 
} 

func fetchUser() { 

     FIRDatabase.database().reference().child("users").queryOrdered(byChild: "attribute1").queryEqual(toValue: "art").observe(.childAdded, with: { (snapshot) in 

      if let dictionary = snapshot.value as? [String: AnyObject] { 
       let user = User() 

       user.setValuesForKeys(dictionary) 

       self.users.append(user) 

       DispatchQueue.main.async(execute: { 
        self.tableView.reloadData() 
       }) 
      } 

     }, withCancel: nil) 
    } 

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

     let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell 

     let user = users[indexPath.row] 
     cell.textLabel?.text = user.name 
     cell .detailTextLabel?.text = user.email 

     if let profileImageUrl = user.profileImageUrl { 

      cell.profileImageView.loadImageUsingCachWithUrlString(urlString: profileImageUrl) 
     } 

     return cell 
    } 

回答

2

一個(大,在我看來)火力地堡的缺點是不能按多個鍵一次篩選。執行此操作的一般策略是按照您認爲會爲您提供最小數據集的密鑰進行過濾,並通過客戶端上的其餘密鑰進行過濾。所以,舉例來說,如果你認爲你的當前過濾器會給你最小的一組(或者,如果他們都會給大約相同的),您將追加用戶之前,請檢查:

if user.attribute2 == "music" && user.attribute3 == "sports" { 
    self.users.append(user) 
} 

您還可以篩選一旦你擁有了所有的用戶,就可以列出。但關鍵是Firebase目前不允許進行這種過濾,因此您需要在客戶端進行此操作。

self.users = self.users.filter({ $0.attribute2 == "music" && $0.attribute3 == "sports" }) 
相關問題