Is there a way to retrieve data from firebase with limit and offset? For example I have about 1000 elements in my firebaseRef and want to develop some kind of pagination. Is
Yup, paging through a long list of child nodes is definitely possible. The trick is to remember the key (or priority or whatever value you sort on) of the last item on the current page and then pass that into queryStartingAt...
for the next page.
Kato wrote up a great example in his blog post on how to implement common SQL queries in Firebase:
// fetch page 2 of widgets
new Firebase("https://examples-sql-queries.firebaseio.com/widget")
.startAt(null, lastWidgetOnPrevPage)
.limitToFirst(LIMIT+1) // add one to limit to account for lastWidgetOnPrevPage
.once('value', function(snap) {
var vals = snap.val()||{};
delete vals[lastWidgetOnPrevPage]; // delete the extraneous record
console.log('widgets on this page', vals);
});
This example is using the JavaScript SDK, but the relevant methods are also available in the iOS SDK.
Note that this snippet retrieves one extra item (LIMIT+1
), since we start on the last item of the previous page.