Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase retrieve all data on app start

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.

like image 571
Isuru Avatar asked Jul 18 '16 11:07

Isuru


People also ask

Can we retrieve data from Firebase?

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.

What's the method used to retrieve data from a Firebase database?

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.

How retrieve data from Firebase and display in RecyclerView?

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.


2 Answers

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

like image 77
Karthi R Avatar answered Sep 26 '22 02:09

Karthi R


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.

like image 43
Wilik Avatar answered Sep 25 '22 02:09

Wilik