Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase query with limit / offset possible solutions

Tags:

ios

firebase

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 there a way I can do it without loading full list of objects.

For now I'm using queryLimitedToLast(limit) to do it and increase limit for every next page. But this method do not allow me to get pages count.

UPDATE:

One thing I did not mention but it's very important. My data showing from-last-to-first. Imagine it's a simple messenger but with pagination. So I want to show last 20 items, then 20 before these 20 etc.

Thanks.

like image 645
anatoliy_v Avatar asked Dec 29 '14 15:12

anatoliy_v


People also ask

Is there a workaround for the Firebase query in limit to 10?

Your only workaround is to make one query for each item in the array that you would normally use with a single "in" query. Or, batch the requests in the array.

Can Firebase handle 10 million users?

The limit you're referring to is the limit for the number of concurrently connected users to Firebase Realtime Database on the free Spark plan. Once you upgrade to a payment plan, your project will allow 200,000 simultaneously connected users.

What is orderBy in Firebase?

By default, a query retrieves all documents that satisfy the query in ascending order by document ID. You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() .

How to use startAfter in Firebase?

Use the startAt() or startAfter() methods to define the start point for a query. The startAt() method includes the start point, while the startAfter() method excludes it. For example, if you use startAt(A) in a query, it returns the entire alphabet. If you use startAfter(A) instead, it returns B-Z .


1 Answers

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.

like image 103
Frank van Puffelen Avatar answered Oct 10 '22 09:10

Frank van Puffelen