Pagination in iOS chat application

735 views
Skip to first unread message

Eric Schanet

unread,
Jul 14, 2015, 5:40:29 PM7/14/15
to fireba...@googlegroups.com
Hi guys,

Let's say I have a simple chat application where messages are being constantly appended. The different conversations each have unique ID's and contain the different message items that have been sent in this conversation in a chronological order. Each message has some keys that are necessary to correctly display the message. So far for the setup.

Now, the problem is, when a conversation is getting bigger, the download times obviously are getting to long, so I need to implement a pagination system. Here comes the big question: how shall I do that? Any quick suggestion? I already thought about consecutive message ID's, but that's obviously not a good way to get to my goal. Another way would be to maybe sort 50 messages in a page1, the next 50 messages in page2 and so on, but then there's the problem that I don't know how many pages currently exist and that page1 (containing the 50 first messages, thus the "oldest" messages) is not the most recent page if there is more than one page, and that those messages won't be displayed first, but effectively as the last ones when the user taps on a "load more messages" button.

So, is there anyone who's had that problem and can come up with an easy solution I didn't think of yet?

Best regards,
Eric

Kato Richardson

unread,
Jul 14, 2015, 10:21:05 PM7/14/15
to fireba...@googlegroups.com
Eric,

An infinite scroll strategy (e.g. your load more message button), seems like the easiest approach, given that the total number of pages is difficult to calculate. There are a few useful techniques for getting a page count (e.g. maintain a counter, use shallow=true), but infinite scroll definitely is an easier model for now.

As for sorting the most recent first, this would be done by adding a timestamp to the records (you want to invert it to get descending order; e.g. record.timestamp = 0 - Date.now();). With that in place, you can utilize queryOrderedByChild to sort the records and then limitToFirst() to paginate.

Cheers,
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/b9867d7c-0cb5-4533-b768-1f3cb9eea772%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Schanet

unread,
Jul 15, 2015, 3:30:47 AM7/15/15
to fireba...@googlegroups.com
Hi Kato,

Thanks for your fast replay. The infinite scroll is something I currently have in place. However, with conversations getting longer and longer, the loading times are getting bigger and bigger, up to a point where the app lags and freezes for a few seconds upon loading the messages.

Any thought about this?

Cheers,
Eric

Kato Richardson

unread,
Jul 15, 2015, 1:03:15 PM7/15/15
to fireba...@googlegroups.com
Yes, in fact. I've dealt with similar things when building the Scroll class for firebase-util.

Check out this article on infinite scroll, which discusses the concept of windowing. Essentially, you only want to render the elements that are visible or soon to be. 

Cheers,
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.

Eric Schanet

unread,
Jul 15, 2015, 5:07:50 PM7/15/15
to fireba...@googlegroups.com
Hi Kato,

Thanks for your answer. I'll have a look at the link and see if I can solve my problem ;-)

Cheers,
Eric
Reply all
Reply to author
Forward
0 new messages