-1

I want to get the data stored in the DB without being restricted to access it only when there is a data change.

I've seen this post from 2016: How to access Firebase data without using addValueEventListener Which suggested to use addValueEventListener. I've also seen this post: Accessing data in Firebase databse Without good answer.

ValueEventListener will trigger the onDataChange only when the database will have a change.

How else can I access the database without something being changed in the database?

For now I will write simple harmless change in order to access the data, but i'm wondering if it's the only way to do it. Thanks

jonb
  • 845
  • 1
  • 13
  • 36
  • 1
    You are required to use a listener to retrieve data from the database when using the Android APIs. – Doug Stevenson Feb 19 '18 at 22:41
  • 1
    The listener is called first time when you add it and the data is available. The following calls will be for further data changes, but you always get the initial data. – algrid Feb 19 '18 at 23:29
  • @algrid I think you are wrong. I've checked it, Had breakpoint inside the OnChildChanged - and it was called only when the value changed – jonb Feb 20 '18 at 08:22

1 Answers1

1

Of course this is absolutely not true. You can retrieve data whenever you like to.

Firstly I would like to advice you to read this documentation reference.

Secondly I provide you with what you really asked for.

If you read the documentation you will notice that it states the following:

The onDataChange() method in this class is triggered once when the listener is attached and again every time the data changes, including the children.

That means that with this code:

databaseReference.removeEventListener(eventListener);

With that method you would be able to detatch any listener so it only listens once or detatch it whenever you want to.

There is a method for only retrieving data once though.

databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.d(TAG, "Data retrieved.");
    }
    ...
}

This method will exactly call onDataChange once or respectively onCancelled.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • I've read the docs, Also, I checked it with breakpoint at onDataChanged, and I didn't receive data until something was changed in the DB. I'll try again later today. I'll update here. Thanks – jonb Feb 20 '18 at 08:24