I have a method that returns true if a child is present in the database and false if not. It looks like this:
boolean subscriber;
public boolean checkChatRoomMembership(String chatRoomUid) {
mChatRoomMembers.child(chatRoomUid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(mAuth.getUid())) {
subscriber = true;
} else {
subscriber = false;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return subscriber;
}
Even though the the node is present in the database it returns false the first time the method is ran. The following times it returns true as it should. It is always the first time the activity is started that it returns false. Any ideas why?