如果值等于... fetch,则为swift 3 firebase快照

时间:2017-08-02 21:13:56

标签: ios swift firebase firebase-realtime-database snapshot

enter image description here

我试图检查房间是否有价值'所有者'如果是,则等于当前用户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 个答案:

答案 0 :(得分:2)

您错过了查询中的值:

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

请参阅此更多查询运算符documentation on filtering data

答案 1 :(得分:0)

Mark:-迅捷5

Database.database().reference().child("user")
        .queryOrdered(byChild: "UserPhoneNumber") //in which column you want to find
        .queryEqual(toValue: "Your phone number or any column value")
        .observeSingleEvent(of: .value, with: { (snapshot) in

            if snapshot.childrenCount > 0
            {
                if let snapShot = snapshot.children.allObjects as? [DataSnapshot] {
                    //MARK:- User Exist in database
                    for snap in snapShot{
                        //MARK:- User auto id for exist user
                        print(snap.key) 
                        break
                    }
                }

            }
            else if snapshot.childrenCount == 0
            {
                //MARK:- User not exist no data found
            }
    })