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

问题描述 投票:0回答:2

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)

        }
    })
ios swift firebase firebase-realtime-database snapshot
2个回答
2
投票

您错过了查询中的值:

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

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


0
投票

马克: - 斯威夫特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
            }
    })
© www.soinside.com 2019 - 2024. All rights reserved.