Firebase - How can I retrieve the objects in a date range?

扶醉桌前 提交于 2019-12-21 23:46:33

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/38413752/firebase-how-can-i-retrieve-the-objects-in-a-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!