I'm trying find out if a Firebase key exists using Firebase Android Api. Here is the code snippet:
Firebase ref = new Firebase("https://[MYFB].firebaseio.com/");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists()){
dataSnapshot.getKey();
}
}
//...
@Override
public void onCancelled(FirebaseError err) {
}
});
The problem with this approach is that I can't get a 'DataSnapshot' without resorting to asynchronous callback 'ChildEventListener()'. Since the call is asynchronous, my code does not know if the key doesn't exist or the onChildAdded() has not been consumed yet.
When searching for a solution I found the method '.once()' in JS Api that would solve the problem, but there is no 'synchronous' equivalent in the Android Api.
Am I missing something obvious?