【问题标题】:swift 3 firebase snapshot if value equal to ... fetchswift 3 firebase 快照,如果值等于 ... fetch
【发布时间】:2023-04-05 21:52:01
【问题描述】:

我正在尝试检查房间的值“所有者”是否等于当前用户 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


    【解决方案1】:

    您缺少查询中的值:

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

    请参阅documentation on filtering data,了解更多查询运算符。

    【讨论】:

      【解决方案2】:

      马克:- Swift 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
                  }
          })
      

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 2016-06-11
        • 2017-05-29
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2016-05-27
        • 1970-01-01
        相关资源
        最近更新 更多