I'm writing a method where I want to query a database and perform another method after the query has finished. Since the call to the database is asynchronous, though, I can't seem to figure out how to stop the main flow of execution until the database call is complete, like so:
public void getUserFromEmail(String email) {
Firebase usersRef = rootRef.child("users");
//Asynchronous database call, want to stop everything else
usersRef.orderByKey().equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println(firebaseError.getMessage());
}
});
//Perform some more operations after database call is finished
}
I'm used to working with asynchronous callbacks in Nodejs, but I can't figure out if there's any equivalent way of controlling execution flow in Java. Is there any easy way to achieve what I want to do?