2

I have a list of data having date. I usually query that data sorted by date. But I am in a situation when I need to retrieve some data back and forth from the middle key. I have data sorted by date (orderByChild("date")) like this:

{
    "key1":{
        "date": "2016-07-25"
    },
    "key2":{
        "date": "2016-07-26"
    },
    "key3":{
        "date": "2016-07-27"
    },
    "key4":{
        "date": "2016-07-28"
    },
    "key5":{
        "date": "2016-07-29"
    }
}

Note: Dates can be in future also. There can be multiple entries for each date. There can be a gap between two dates.

If I know the key3, How can I retrieve data from one above key3 (i.e. key2) to end of the list (i.e. key5)?

So for example,

  • with key3 -> key2 to key5 (till end).
  • with key4 -> key3 to key5 (till end).

How can I query it with key, and order by child "date". I have a key2. And from that I need to fetch the list.

I have tried to fetch results in parts, by one for upper and one for lower. But, when I use orderByChild("date"), I can not use endAt() with keys (Docs says: you can combine the startAt() and endAt() methods to limit the results to a specified range of values).

Is there any way I can retrieve the list based on key? In a single go or in parts?

kirtan403
  • 7,293
  • 6
  • 54
  • 97

1 Answers1

0

I have changed the way keys are defined.

Instead of push(), I have set the key with the combination of date and push().

Like, the new key is in the following format YYYYMMDD<PUSH_KEY>. With this format, I am able to query data with 2 separate queries.

One ending with the key:

ref.orderByKey().endAt(key).limitToLast(1)

and one starting with the key:

ref.orderByKey().startAt(key)

This gives me the results from both the ends from the middle key.

Thank you @puf and everyone for the comments and help.

kirtan403
  • 7,293
  • 6
  • 54
  • 97
  • @Kirten403 ... I have also stacked in the same problem..What is my requirement is that..I want to retrieve only 10 records at a time and by pull to refresh remaining 10 record , i want to fetch until i reached at the end of the data in Firebase...Could you plz help or give your suggestion on it – Ravindra Kushwaha Feb 27 '17 at 12:55
  • @RavindraKushwaha For that you may need to implement your own login in the recycler adapter or list view based on your requirement. When user pull, make a query to retrieve the record by maintaining the start key and limit your query to 10 records. Append every time you get the result to your original list of result. Hope you got the point I want to say.. – kirtan403 Feb 28 '17 at 05:03
  • @Kirten403 ....Yet now i have login and getting first time that at limit of 10..Now i want to retrieve 10 more records on load more...Could u plz help me on it...I have created the logic with start,limit and other methods of firebase but did not get the expected result.. – Ravindra Kushwaha Feb 28 '17 at 05:48
  • @RavindraKushwaha Sure, when you want to load 10 more records on load more, get the last key of the already loaded items and use that key in the startAt(key) to retrive more 10 from that location – kirtan403 Feb 28 '17 at 05:52
  • thanks for your valueable comment...key be like KdzBv65wIBDYDdXetog in firebase?? Please look at my firebase database http://tinypic.com/r/w1g0w2/9 – Ravindra Kushwaha Feb 28 '17 at 06:14
  • @RavindraKushwaha Yes. This will work. Just use the last key for the startAt(). It should work as intended. startAt and limit will get your work done. Try and let me know – kirtan403 Feb 28 '17 at 06:25