public List<Song> retrieveSongs() {
List<Song> dbSongs = new ArrayList<>();
db = FirebaseDatabase.getInstance().getReference().child("songs");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
dbSongs.clear();
for (DataSnapshot s : snapshot.getChildren()) {
Song song = s.getValue(Song.class);
dbSongs.add(song);
}
Log.d("SONGSSSS", String.valueOf(dbSongs.size()));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return dbSongs;
}
the size of the returned list dbSongs is always 0 although it's not in the log statment as if when im out of the listener, the list is cleared. and i dont understand why. i even made sure that the snapshot contains data and it does.
i tried to retrieve an array of Song object from the database that looks like the following:
songs:
|song1
|song2
|song3
i made sure that i am retrieving the correct snapshot of data that i want. but when the function returns, it is returning an empty array.