ahmed samir
ahmed samir

Reputation: 7

Android Firebase retrieve all data without any listener

How can I retrieve all data values from Firebase's real-time database when I load onStart event from my Android app without run any listener of Firebase?

Because I don't want to run any listener such as ("ondataChange, childAdded, childRemoved and etc ...") I don't need it.

Upvotes: 0

Views: 2450

Answers (3)

urOutsourced
urOutsourced

Reputation: 1047

From the Firebase documentation, the onDataChange method of the listener fires once when being attached and then every time there is a change. So, yes there is a way to fetch the data without an event. Though, this fetch would happen using listener only.

https://firebase.google.com/docs/database/android/read-and-write

Listen for value events To read data at a path and listen for changes, use the addValueEventListener() oraddListenerForSingleValueEvent() method to add a ValueEventListener to a DatabaseReference.

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. The event callback is passed a snapshot containing all data at that location, including child data. If there is no data, the snapshot will return false when you call exists() and null when you call getValue() on it.

Upvotes: 2

jrise
jrise

Reputation: 26

Though, it is late to reply here, but I am choosing to post here only rather than creating another thread on this.

IMO, the basic expectation from any database is to be able to read a selected set of data which is available in DB. It does not matter whether I have to attach a listener or write a custom code.

While I understand that the added benefit of real-time DB is that it is event driven and UI does not need to wait for user to initiate an action for getting the data, but how come there is no way to retrieve data if there is a need !! I have spent hours looking for this one way to fetch data. OOh :(

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

There is no way to get the data from a Firebase real-time database or from Cloud Firestore without using a listener. Everything in Firebase is about listeners, even if you are getting data in real-time or if you are getting only once, you need to attach a listener on a particular location. Without it, there is no way you can get it.

Edit: From the official documentation regarding reading data in Android, here is a code snippet that demonstrates a social blogging application retrieving the details of a post from the database:

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.addValueEventListener(postListener);

Upvotes: 0

Related Questions