Firebase - 如何检索日期范围内的对象? [英] Firebase - How can I retrieve the objects in a date range?

查看:144
本文介绍了Firebase - 如何检索日期范围内的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase中的下一个JSON结构:

  {
-KGX6kYg1NO6d9hn-8um:{
phase:A,
timestamp:12-18-2015 19:43:37
},
-KGXOGSxa3vompZX9UO_:{
phase:B,
timestamp:03-28-2016 15:28:21
},
-KMUvszD-vm3Nu02sofd:{
phase:A,
timestamp:04-03-2014 03:57:56
}
}

是否可以使用timestamp键过滤日期范围内的对象?例如,我想从1月份获取带有时间戳的对象2015年至今日期。如果没有可能,有什么更好的方法来过滤对象的日期?...我正在开发一个iOS应用程序。

谢谢。

$ b $您可以通过 timestamp 对快照数据进行排序,并定义一个时间戳 / code>限制你想要的数据。
例如,您需要特定时间戳 timestamp1 中的所有数据,则您的引用处理程序应如下所示:

<$对于快照中的项目,在
中使用obserEventType(FIRDataEventType.Value,withBlock:{(快照)){code> let refHandle = tableRef.queryOrderedByChild(timestamp)。queryEndingAtValue(timestamp1 .children {

$ b})

您也可以通过添加 queryLimitedToLast queryLimitedToFirst 来应用您想要的记录数量:

  let refHandle = tableRef.queryOrderedByChild(timestamp)。queryEndingAtValue(some_time_stamp)。queryLimitedToLast(kPostLimit + 1).observeEventType(FIRDataEventType.Value,withBlock:{ 
for snapshot.children {
$ b}
})

另外,您还需要查看以下文章关于Firebase中的常见SQL查询。


I have the next JSON structure in Firebase:

{
  "-KGX6kYg1NO6d9hn-8um" : {
    "phase" : "A",
    "timestamp" : "12-18-2015 19:43:37"
  },
  "-KGXOGSxa3vompZX9UO_" : {
    "phase" : "B",
    "timestamp" : "03-28-2016 15:28:21"
  },
  "-KMUvszD-vm3Nu02sofd" : {
    "phase" : "A",
    "timestamp" : "04-03-2014 03:57:56"
  }
}

Is it possible to filter the objects by the timestamp key through a range of date?.. For example, I want to get the objects with timestamp from January 2015 to today date. If not possible, what's the better way to filter the objects by dates?... I'm developing an iOS app.

Thanks.

解决方案

You can sort the snapshot data by timestamp and define a timestamp limit from which you want your data. For example you want all data from a specific timestamp timestamp1, your reference handler should look like:

let refHandle = tableRef.queryOrderedByChild("timestamp").queryEndingAtValue("timestamp1").observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
            for item in snapshot.children {

            }
        })

You can also apply number of records that you want by adding queryLimitedToLast or queryLimitedToFirst like :

  let refHandle = tableRef.queryOrderedByChild("timestamp").queryEndingAtValue("some_time_stamp").queryLimitedToLast(kPostLimit + 1).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
            for item in snapshot.children {

            }
        })

Also, you want to have a look at the following post about common sql queries in Firebase.

这篇关于Firebase - 如何检索日期范围内的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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