0

I created a project in which I need to listen to multiple database locations at once.

{
  "A": [data-a],
  "B": [data-b],
  "C": [data-c]
}

Whenever anything gets changed, added or deleted at location A, B or C I want to display the action which happened inside a ListView in Android.

For one location I have been able to achieve this by adding the following code to my project.

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("A");
databaseReference.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    //Child got added
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    //Child got changed
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    //Child got removed
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    //Child got moved
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    //Some error happened
    }
});

Now to my question. Do I need multiple listeners for each location or can I somehow get notified about all changes in a simpler way?

Rodrigo Ehlers
  • 1,830
  • 11
  • 25
Farrukh Faizy
  • 1,203
  • 2
  • 18
  • 30
  • See also https://groups.google.com/forum/#!searchin/firebase-talk/join|sort:date/firebase-talk/DKnBnI0bqoI/YMrp-L1hBgAJ – Kato Jan 30 '17 at 19:16

1 Answers1

1

Yes, you would need to add multiple references and child listeners. Firebase has no concept of unions, joins, or other operators present in SQL databases. There is a 1:1 relationship between collections of data you want to retrieve and the references / listeners you use to access them. You cannot use wildcards or other mechanisms to access more than one data collection at a time from a single call.

Chad Robinson
  • 4,575
  • 22
  • 27
  • Alright! But isn't it ambiguous to add , For suppose 3 child listener for 3 db references. Do i need to implement 1:1 Child-Listener for every db reference? – Farrukh Faizy Jan 31 '17 at 12:31
  • I'm not sure what you mean by ambiguous, but this is standard Firebase practice. If you look at FirebaseUI you will even find "indexed" data source implementations that do exactly this - make one master query, then for each child, obtain that child's ref separately. – Chad Robinson Jan 31 '17 at 18:16