If you call a Firebase method like below and then create a while loop which waits for a change to be made by this method, onDataChange
is never invoked. The while loop prevents this from happening. But, why does this happen? Shouldn't asynchronous method like onDataChange
do its job regardless of what you do outside of this method?
The method I am talking about :
public static void readLeagueStandings(){
DatabaseReference leagueStandingsDbRef = firebaseDatabase.getReference("league-standings");
leagueStandingsDbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
LeagueStandings leagueStandings = objectMapper.convertValue(snapshot.getValue(), LeagueStandings.class);
LocalInfo.leagueStandings.add(leagueStandings);
}
leagueStandings = true;
}
@Override
public void onCancelled(DatabaseError error) {
error.toException();
}
});
}