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?
Automatically scales with your app. When your app is a breakout hit, you don't have to worry about scaling your server code or provisioning extra capacity — Firebase handles that automatically for you. Our servers manage millions of >concurrent connections and billions of operations per month.
Firebase services encrypt data in transit using HTTPS and logically isolate customer data. In addition, several Firebase services also encrypt their data at rest: Cloud Firestore.
Firebase Realtime Database is a great fully managed, low latency and zero-configuration database for real-time applications.
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)...
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.
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