I am using Firebase for the chat feature in my Android application. What I am aiming to support is:
The first part seems easy. I create a firebase reference with a limit.
ref = new Firebase(FIREBASE_URL).child("chat").limitToFirst(50);
This reference is then used by an Adapter which adds a listener. And then modifies a List whenever data changes.
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {...}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {...}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {...}
...
The second part is where I struggle to find the right approach. When the user arrives at the end of the list, I want to load older messages and add them to the list of messages (infinite scroll).
The preferred solution would be to simply call something like ref.changeLimitToFirst(100)
but such a method does not exist.
What would be the best approach to solve this with firebase?
Edit:
I deleted the solutions initially suggested by me because they did not help clarifying the problem. Instead, I will list new solutions below which I found since I originally asked the question.
If you know of a better, more elegant approach, please leave an answer!
In Firebase there are two ways to retrieve data:
1) Observe a node via an EVENT
2) Query (within) a node for specific data
The two items are independent functions, but can behave in similar ways and can both retrieve data from Firebase.
With Firebase queries, you can selectively retrieve data based on various factors.
The answer to the question depends on the structure of the data. Assume this structure
Msgs
node_ref:
index: 0
msg: 'Hows the weather'
node_ref:
index: 1
msg: 'Hot'
Answer to: 1) Live Updates
If the Msgs node has a listener, the app is notified of any changes to that node and could be used to load an initial snapshot of the data within that node and then listen for add's, updates and removes to messages within that node.
Based on your update, you have a way to load the first 50. However, the answer to part #2 may be another solution.
Answer to: 2) scrolling backwards (or forwards, or loading in sets of data)
Adding a listener would be independent of performing a Firebase Query on the Msgs node for specific messages. A Query would be used to load in a message with a child index of 1, or any message with child index of less than 10 or messages that contain child indexes in a range of starting at 5 and ending at 10.
This could be done with a Range Query: Using startAt(), endAt(), and equalTo() allows you to choose arbitrary starting and ending points for queries.
So if the Msgs node contained 100 messages, and messages with a child index of 90-99 are displayed and the user scrolls backward (to older messages) a simple Range Query could be done to query for messages 80-89, which would then be loaded and displayed. Similarly if messages 0-9 are displayed (the oldest) and the user scrolls forward, Range Query from messages 10-19.
As you can see, a listener is not needed at all when leveraging Queries to load the data. The listener would be used to tell the app when there is new data (added, updated or deleted)
That being said, there are ways to do this with no queries but it would depend on the structure of the data and how you want to UI to behave.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With