2017-08-02 55 views
0

enter image description hereSWIFT 3火力點快照,如果值等於...取

我想檢查房間的值「所有者」等於當前用戶ID,如果是的話獲取所有數據,包括密鑰值和繼續檢查'房間'的其他孩子

我正在嘗試,但我找不到解決方案,雖然它可能看起來很容易,所以請幫助我的建議或想法。到目前爲止我的代碼:

  Database.database().reference().child("rooms").queryOrdered(byChild: "Owner").observeSingleEvent(of: .value, with: { (snapshot) in 

     let currentUser = Auth.auth().currentUser?.uid 

     if !snapshot.exists() { 
      print("No data found") 
      return 
     } 

     var rooms = snapshot.value as! [String:AnyObject] 
     let roomKeys = Array(rooms.keys) 

     for roomKey in roomKeys { 

      guard 

       let value = rooms[roomKey] as? [String:AnyObject] 

       else 
      { 
       continue 
      } 

      let title = value["title"] as? String 
      let description = value["description"] as? String 
      let roomPictureUrl = value["Room Picture"] as? String 
      let longitude = value["Longtitude"] as? String 
      let latitude = value["Latitude"] as? String 
      let dateFrom = value["Date From"] as? String 
      let dateTo = value["Date To"] as? String 
      let owner = value["Owner"] as? String 

      let myRooms = Room(roomID: roomKey,title: title!, description: description!, roomPicutreURL: roomPictureUrl!, longitude: longitude!, latitude: latitude!, dateFrom: dateFrom!, dateTo: dateTo!, owner: owner!) 

      self.rooms.append(myRooms) 
      self.tableView.reloadData() 

      print(snapshot.value) 

     } 
    }) 

回答

2

你錯過了在查詢中的值:

Database.database().reference() 
    .child("rooms") 
    .queryOrdered(byChild: "Owner") 
    .queryEqual(toValue: "STbz...") 
    .observeSingleEvent(of: .value, with: { (snapshot) in 

見,這和多個查詢運營商來說,documentation on filtering data