I have trouble finding any information using firebase documentation and google on how to retrieve all data on app start.
Let's assume, I want to get all the following data on app start without any event. and store in a ArrayList. Let's say ArrayList<Quote>
and quote has three fields (int id, String text, String author).
[{
"id" : 1,
"text": "There are 1,411 tigers left in India.",
"author": "NULL"
}, {
"id" : 2,
"text": "The Greek for \"left-handed\" also means \"better\".",
"author": "NULL"
}]
According to the documentation there is a method called onDataChange() but, app is not changing any data. How to grab all data and store in ArrayList<Quote>
which I can pass to custom adapter.
Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.
Blocking reads: Data stored in a Firebase Realtime Database is retrieved by invoking a blocking method on a database reference, which returns the data stored at the reference. Each method call is a onetime operation. That means the SDK does not register any callbacks that listen to subsequent data updates.
Step 1: Open Android Studio and create a new project named “RecyclerView” with an empty activity. Step 2: Connect your Firebase project with your app. Step 3: Add the following dependency in your app/build. gradle file in order to get the FirebaseUI and Firebase Realtime Database support in the app.
Keeping Data Fresh
The Firebase Realtime Database synchronizes and stores a local copy of the data for active listeners. In addition, you can keep specific locations in sync.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
The client will automatically download the data at these locations and keep it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.
Refer this doc for more info; https://firebase.google.com/docs/database/android/offline-capabilities
The onDataChange()
method is called directly when the listener (ValueEventListener
or ChildEventListener
) is attached, so you don't need to do any changes to the data to trigger that method.
From the documentation Firebase - Retrieve Data on Android
This method is triggered once when the listener is attached and again every time the data, including children, changes.
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