I am adding a listener to a node within my database as follows:
ref = myDB.getReference("testNode/" + uID);
...
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d("PROFILE", singleSnapshot.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
Here is my database structure:
"testNode" : {
"ffggg@gmailcom" : {
"kcd8huka0bha6md22nh1k5enj0" : 1484814690039,
"vd4vk67n2gb7qgpol0jp8gee62" : 1484814727049
},
"test22@gmailcom" : {
"8o93lpc5clhrsrst852nfm2g75" : 1484815514788,
"a18g2u5h525nqthpqsn6askp75" : 1484814852458
}
}
As you can see above I have a Log for testing, the Log prints as follows:
01-19 08:44:57.547 17531-17531/com.firebase.android D/PROFILE: ffggg@gmailcom 01-19 08:44:57.547 17531-17531/com.firebase.android D/PROFILE: test22@gmailcom 01-19 08:45:15.446 17531-17531/com.firebase.android D/PROFILE: 8o93lpc5clhrsrst852nfm2g75 01-19 08:45:15.446 17531-17531/com.firebase.android D/PROFILE: a18g2u5h525nqthpqsn6askp75 01-19 08:45:15.448 17531-17531/com.firebase.android D/PROFILE: ffggg@gmailcom 01-19 08:45:15.449 17531-17531/com.firebase.android D/PROFILE: test22@gmailcom 01-19 08:45:15.587 17531-17531/com.firebase.android D/PROFILE: 8o93lpc5clhrsrst852nfm2g75 01-19 08:45:15.587 17531-17531/com.firebase.android D/PROFILE: a18g2u5h525nqthpqsn6askp75 01-19 08:45:15.588 17531-17531/com.firebase.android D/PROFILE: ffggg@gmailcom 01-19 08:45:15.588 17531-17531/com.firebase.android D/PROFILE: test22@gmailcom
Now assuming uID is test22@gmailcom
(and i have checked the string is not null or empty) I should only get test22@gmailcom
's children. Yet here I get all nodes under ref (but not their children, unless the node is uID). I dont want this because then I download lots of unnecessary data if I had thousands upon thousands of records.
Am i doing something wrong or was the Listners implemented like this by firebase?
Update 1
When I open the application from the app drawer it will get the correct children of the node I want. However, when I go to another activity inside of this activity I add a child to the node I am listening on in the other class and then go back to the class which is listening on that node I get the weird output.
Here is the code for the other activity where I add to the node:
pullNewImage = myDB.getReference("testNode");
pullNewImage.child(uID).child(randomName).setValue(ServerValue.TIMESTAMP);
Update 2
My updated listener code:
//global var
ValueEventListener profileListener = null;
//....
profileListener = ref.child(userEmail).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d("PROFILE", singleSnapshot.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
//...
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
auth.removeAuthStateListener(mAuthListener);
}
if(profileListener != null)
ref.removeEventListener(profileListener);
}