【问题标题】:Querying Firebase with Swift 3.0使用 Swift 3.0 查询 Firebase
【发布时间】:2017-03-25 07:32:39
【问题描述】:

我正在尝试获取createdAt 等于Today 的所有条目,但它什么也不返回。

我在这里做错了什么?而且,以我尝试的方式查询数据的正确方法是什么?

JSON:

    {
      "thoughts" : {
        "-KWGdcdZD8QJSLx6rSy8" : {
          "createdAt" : "Tomorrow",
          "thought" : "meno",
          "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2"
        },
        "-KWGeGivZl0dH7Ca4kN3" : {
          "createdAt" : "Today",
          "thought" : "meno",
          "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2"
        },
        "-KWGeIvWHBga0VQazmEH" : {
          "createdAt" : "Yesterday",
          "thought" : "meno",
          "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2"
        }
      }
    }

斯威夫特:

    let db = FIRDatabase.database().reference().child("thoughts")
    let ref = db.queryEqual(toValue: "Today", childKey: "createdAt")

    ref.observe(.value, with:{ (snapshot: FIRDataSnapshot) in
        for snap in snapshot.children {
            print((snap as! FIRDataSnapshot).key)
        }
    })

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    我在 Swift 5 中的解决方案,希望这会有所帮助。

    let ref = Database.database().reference().child("thought")
    let refHandle=ref.queryOrdered(byChild:"thoughts").queryEqual(toValue:"Today").observe(.value, with: { (snapshot) in
    if let snapshot = snapshot.children.allObjects as? [DataSnapshot]{
    let snap = snapshot.value as? [String:Any]
    print(snap)
    }
    })
    

    【讨论】:

      【解决方案2】:

      这是我对这个 JSON 的解决方案:

      JSON:

      {
        "users" : {
          "-KWGdcdZD8QJSLx6rSy8" : {
            "name" : "dummy1",
            "visibility" : true
          },
          "-KWGeGivZl0dH7Ca4kN3" : {
            "name" : "dummy2",
            "visibility" : false
          },
          "-KWGeIvWHBga0VQazmEH" : {
            "name" : "dummy3",
            "visibility" : true
          }
        }
      }
      

      斯威夫特:

      //只获取属性为“visibility”=true的子项

      Database.database().reference().child("users").queryOrdered(byChild: "visibility").queryEqual(toValue: true).observeSingleEvent(of: .value, with: { (snapshot) in
      
      
              guard let dictionary = snapshot.value as? [String:Any] else {return}
      
              dictionary.forEach({ (key , value) in
      
                  print("Key \(key), value \(value) ")
      
      
              })
      
      
      
          }) { (Error) in
      
              print("Failed to fetch: ", Error)
      
          }
      

      【讨论】:

        【解决方案3】:

        您需要使用 queryOrderedByChild 到 createdAt 而不是使用 equalTo Today

        let ref = FIRDatabase.database().reference().child("thoughts").queryOrdered(byChild: "createdAt").queryEqual(toValue : "Today")
        
        ref.observe(.value, with:{ (snapshot: FIRDataSnapshot) in
            for snap in snapshot.children {
                print((snap as! FIRDataSnapshot).key)
            }
        })
        

        【讨论】:

        • 为遇到此问题的其他人更新此行:let ref = db.queryOrdered(byChild: "createdAt").queryEqual(toValue : "Today")
        • @ColbyGatte 我刚刚编辑了参考线...你能再测试一次吗
        • 嗨@EICaptainv2.0,我实现了你的答案,但我得到了所有的子节点。我使用的是 .childAdded 而不是 .value,我得到了所有的子值,而不仅仅是那些与查询匹配的值(我按 Int 时间戳排序,并使用 queryStarting(at:)。提前感谢任何光你可以脱落。
        猜你喜欢
        • 2017-01-30
        • 2017-02-20
        • 2018-03-05
        • 1970-01-01
        • 2016-10-14
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        相关资源
        最近更新 更多