Chiranjeev Jain
Chiranjeev Jain

Reputation: 99

How do I get database from Firebase Realtime Database for the first time the android app is installed

I know how I can add listeners on database references to detect when there's a change in the data and retrieve that data. But I can't seem to find out how I can get data from firebase when the app is first installed on the user's device. Its not going to happen that the user's installs the app and the data is going to change. How do I get the data without triggering the listeners for this scenario?

Upvotes: 1

Views: 252

Answers (1)

robsiemb
robsiemb

Reputation: 6354

Listeners are triggered once when the listener is attached, and once when the relevant event happens.

So, just by creating a ValueEventListener, you'll get a snapshot of all its current data (and you'll get it every time you attach, not just once after you install the app).

There's more in the documentation about how to listen for value events, but the specifically relevant text is:

You can use the onDataChange() method to read a static snapshot of the contents at a given path, as they existed at the time of the event. This method is triggered once when the listener is attached and again every time the data, including children, changes.

This is also true for ChildEventListeners, via the onChildAdded method. It is called for all items that exist, then once each time an item is actually added. See here.

Upvotes: 1

Related Questions