StartAt(x) and EndAt(y) for pagination

477 views
Skip to first unread message

Dave Dawson

unread,
May 10, 2016, 9:38:12 PM5/10/16
to Firebase Google Group
I'm attempting to grab a number of records, but I'm pretty confused on how to do this. The end goal is to lazy-load notes in a list. So first off, I'd like to grab the first 15 records in a user's Notes list. Eventually I will grab the next 15 notes to append to the list.

I found this post, but it looks like this may be a different case. 

Ideally, I'd like to be able to do something like this:

var ref = firebaseRef.child('users/' + uid + '/notes');

var query = ref.orderByChild('date_updated').startAt(null, start).endAt(null, end);

Am I misunderstanding how startAt and endAt work? Is that second argument for the placement in the order of the `ref` array?

Kato Richardson

unread,
May 10, 2016, 9:48:11 PM5/10/16
to Firebase Google Group
Hi Dave,

startAt() just takes one argument. See the query guide and the API doc for examples and usage. The second argument is for using a legacy feature (priorities).

If you want the first 15, then try startAt(start).limitToFirst(15).

☼, Kato



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/b4e0ac52-84a0-4727-b66e-e23685d235a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Dave Dawson

unread,
May 10, 2016, 9:51:20 PM5/10/16
to Firebase Google Group
Thanks for the quick response, Kato. 

How about the case where I'd like to get posts between 15-29? I'm not sure I understand how that works, based on that documentation.

startAt(15).endAt(29) doesn't seem to work.

Kato Richardson

unread,
May 10, 2016, 9:57:57 PM5/10/16
to Firebase Google Group
For page 2, use startAt() the last value page 1. See this example, this example, and Firebase.util.Paginate.


For more options, visit https://groups.google.com/d/optout.

Dave Dawson

unread,
May 10, 2016, 11:19:09 PM5/10/16
to Firebase Google Group
Firebase.util looks helpful. But is it ready to use in production? Is there a way to do this without adding an additional library?

Kato Richardson

unread,
May 10, 2016, 11:37:15 PM5/10/16
to Firebase Google Group
Dave, check out the other links. One is a vanilla implementation.


For more options, visit https://groups.google.com/d/optout.

Dave Dawson

unread,
May 11, 2016, 11:07:04 AM5/11/16
to Firebase Google Group
I missed that one. Thanks! But how about Firebase.util? Is that ready to use in production? 

I appreciate your quick responses. Thanks, again! It would be great if these kinds of things were documented in-depth somewhere so I don't have to bug you guys. :)

Dave Dawson

unread,
May 11, 2016, 11:17:07 AM5/11/16
to Firebase Google Group
Looking through this example, I still have the same question as my first post. 

If I have a list of notes, each with unique keys. I want to get the 15th through 29th notes in that list. I don't know how to do that. .startAt(15).endAt(29) doesn't work, because that's looking for something different, maybe a key? But because each key is dynamically generated, I don't know what that will be. 

So how do I say: From the full list of notes, only grab the 15th through 29th notes?

Thanks

Jethro Bodine

unread,
May 11, 2016, 1:05:38 PM5/11/16
to Firebase Google Group
I believe you'll have to know the keys at where you'd like to start. For the first page, a simple startAt().limitToFirst([number of results]). Then you'll have to store the key of the last record you pulled to start there next. Say for instance the pushId key for the last one is "txc56blp", I found a simple workaround* to start at the record right after is to add a character to the end of the string so it wont include the previous one... startAt('txc56blp0').limitToFirst(15).
*removing the last character of the key also works in reverse when wanting to endAt a certain point

Unfortunately firebase doesnt provide a simple numerical index yet, as attempting to do so would make the real-time nature much less performant is what I've been told in the past.

Hope that helps

Dave Dawson

unread,
May 11, 2016, 1:54:34 PM5/11/16
to Firebase Google Group
Thanks so much for explaining that, Jethro. Much appreciated! 

I'll give that workaround a shot, but it seems pretty crazy that we need to do something like that to get what I thought would be a pretty simple query. 
Reply all
Reply to author
Forward
0 new messages