2

I am trying to check if a firebase node exists by using a key. E.g. I am looking if node "child_value" exists as a child of "parent_value".

E.g. My databasereference is pointed at "parent_value".
The statement .child("child_value") returns the node of the child value
The statement .hasChild("child_value") returns False

What could cause this odd behaviour where checking .haschild() returns False when the child node does exist?

EDIT:code snippet

//Entire code block below is inside of another singlevaluevent listener, don't know if that would cause any problems

DatabaseReference ref = FirebaseDatabase.getInstance().getReference()
                                       .child("Values");

ref.addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(@NonNull Datasnapshot dataSnapshot){
                     if(dataSnapshot.hasChild(key) {
                           //Would expect the flow to end up here since the child exists
                     } 
                     else {
                           //Flow ends up here
                     }    
                }
FFGH
  • 121
  • 12
  • The first question that pops to mind is if you have disk caching enabled (with `enablePersistence(true)`). If so, using `addListenerForSingleValueEvent` may cause `onDataChange` to be called with a stale snapshot. – Frank van Puffelen Jul 07 '19 at 01:34
  • @FrankvanPuffelen I do indeed have disk caching enbaled. Would the way to go about this be to .keepSynced(False) for the db reference. Or is this not recommended. – FFGH Jul 07 '19 at 09:25
  • 1
    Using disk persistence and single value event listeners don't mix. See https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent/34487195#34487195 – Frank van Puffelen Jul 07 '19 at 12:55

0 Answers0