我有问题,如何从Firebase中获取所有元素,我知道问题是异步操作。但我不知道该如何应对。
public void getStops(MyCallback callback){
firestore.collection("Stops")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
stops.add((String) document.get("name"));
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
callback.onCallback(stops);
}
});
Log.d("stopSizeAfterLoop", String.valueOf(stops.size()));
Log.d("stopref", stops.toString());
}
public void getStopsFromDB(){
db.getStops(new MyCallback() {
@Override
public void onCallback(ArrayList<String> list) {
for(String s : list) {
stops.add(s);
Log.d("stopsInCon", s);
}
}
});
Log.d("stopsizeInCon", String.valueOf(stops.size()));
}
public interface MyCallback {
void onCallback(ArrayList<String> list);
}
我如何获取这些元素以便对其进行处理。现在,我在getStopsFromDB()中有空的Stop列表,因为它获取数据的时间太晚了。是否可以“阻止”所有内容并等待数据?