使用Swift 3.0查询Firebase [英] Querying Firebase with Swift 3.0

查看:53
本文介绍了使用Swift 3.0查询Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get all the entries where createdAt is equal to Today, but it returns nothing.

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

What am I doing wrong here? And, what is the proper way to query data the way I am trying to do?

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)
        }
    })

推荐答案

您需要使用queryOrderedByChildcreatedAt,然后使用equalTo Today

You need to use queryOrderedByChild to createdAt and than use 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)
    }
})

这篇关于使用Swift 3.0查询Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆