I'm writing an android app using java. i have it connected to Realtime database. I'm trying to search my data and check if a certain key exists in it or not.
public boolean checkForKey(String key) {
final boolean[] found = {false};
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()){
if(dataSnapshot.getKey().equals(key)) {
found[0] = true;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
dbRef.child("Polls").addValueEventListener(listener);
return found[0];
}
The function is returning false before it finishes the part of the code that actually searches through the database. Any help in explaining why does this happen is appreciated.