7

If I have a list of 50,000 items stored in my firebase reference, and 5 items have been added to that list since the last time the client was online and listening, which callback would I have to use such that it is only triggered for the 5 new items that have been added?

I have offline persistence enabled on my client with Firebase.getDefaultConfig().setPersistenceEnabled(true);. I have a listener bound to an activity listening for children added to a reference. Everytime the activity is created onChildAdded is called for all the data in the reference. Is it possible to make onChildAdded and onChildRemoved be called only for "diff" between my local cache and the data on the firebase server? Or if that's not possible, then to trigger only those onChild* callbacks after the most recent update from firebase?

2
  • Have you already tried using the onComplete callback for update()? Commented Jan 15, 2016 at 0:40
  • From the android documentation, it looks like updateChildren() is similar to setValue(). I don't know how to use that to figure out what I'm trying to do. I've added an example to the question which might make what I'm trying to achieve clearer. Commented Jan 16, 2016 at 14:22

2 Answers 2

13

Everytime the activity is created onChildAdded is called for all the data in the reference. Is it possible to make onChildAdded and onChildRemoved be called only for "diff" between my local cache and the data on the firebase server?

No, this is not possible. From the documentation on event types:

child_added is triggered once for each existing child and then again every time a new child is added to the specified path

Now back to your initial question:

which callback would I have to use such that it is only triggered for the 5 new items that have been added?

That would be:

ref.limitToLast(5)... 

But this requires that you know how many items were added to the list, since you last listened.

The more usual solution is to keep track of the last item you've already seen and then use startAt() to start firing events from where you last were:

ref.orderByKey().startAt("-Ksakjhds32139")... 

You'd then keep the last key you've seen in shared preferences.

Similarly you can keep the last time the activity was visible with:

long lastActive = new Date().getTime(); 

Then add a timestamp with Firebase.ServerValue.TIMESTAMP to each item and then:

ref.orderByChild("timetstamp").startAt(lastActive+1)... 
Sign up to request clarification or add additional context in comments.

9 Comments

Would this solution also fire childChanged and childRemoved events?
Although Frank's answer is ABSOLUTELY CORRECT, I think you should reconsider the logic in order to handle CREATE, UPDATE and DELETE (based on the actions he suggests). It looks to me that you may consider a Firebase based FIFO journal of C(R)UD actions (with expiration / eviction logic) in order to catch everything that happened when different devices were off-line.
Yes, a FIFO of events since last query would be optimal, but it looks like I'm going to use a childEventListener as is. When capturing events, I'll ignore data that has already been seen by storing it locally.
What about deleted items when off-line?
I liked your last solution but what about the time differences between server and the local device?
|
1

You should use on('child_changed'). Normally, on() is used to listen for data changes at a particular location. However, on('child_changed') notifies you

when the data stored in a child (or any of its descendants) changes.

It will pass a data snapshot to the callback that contains the new child contents. Keep in mind that a single child_changed event may potentially represent multiple changes to the child.

2 Comments

Wouldn't he in this case get a list of all 50000 + 5 items not he 5 he is asking for (see the last paragraph of the question).
You're right, I suppose he could also use limitToLast(x) to get the last X updated children.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.