5

Is there a way to access firebase data without having to use the addValueEventListener? This way, I can access the data whenever I need, as opposed to being restricted to access it only when there is a data change.

I am coding in Java. Thanks :)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
kmindspark
  • 487
  • 1
  • 7
  • 18

1 Answers1

4

The only way to get the value of a location in your database is one of the getValue() methods of DataSnapshot. The only way to get a DataSnapshot is as a parameter of a callback method of one of the listeners: ValueEventListener or ChildEventListener.

You can get the current value of a location one time using Query.addListenerForSingleValueEvent(). The listener callback method, onDataChange(), will fire once with a DataSnapshot that provides the value of the data at the location. To get a callback for the current value and each subsequent change, use Query.addValueEventListener(). To get the current values and changes to the children of a location, use Query.addChildEventListener().

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158