5

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.

Isuru
  • 3,818
  • 13
  • 49
  • 64
  • Reading from the doc, section "Value Events": `onDataChange() [...] This method is triggered once when the listener is attached and again every time the data, including children, changes.` – Devid Farinelli Jul 18 '16 at 11:24

2 Answers2

3

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

Karthi R
  • 1,338
  • 10
  • 25
2

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.

Wilik
  • 7,630
  • 3
  • 29
  • 35