Android 如何在代码中等待所需数据从firestore下载?
我可以从firestore数据库读取数据,但我不能在代码中等待整个数据加载。所以现在发生的是,我的代码甚至在实际加载所需数据之前就向前执行。我想知道如何在从firestore读取数据时等待,一旦数据被提取,那么只执行其他代码。我的代码片段Android 如何在代码中等待所需数据从firestore下载?,android,google-cloud-firestore,Android,Google Cloud Firestore,我可以从firestore数据库读取数据,但我不能在代码中等待整个数据加载。所以现在发生的是,我的代码甚至在实际加载所需数据之前就向前执行。我想知道如何在从firestore读取数据时等待,一旦数据被提取,那么只执行其他代码。我的代码片段 public void getFoodMenu(String clientID){ clientFullMenu.clear(); DocumentReference docRef = mDatabase.collection(&quo
public void getFoodMenu(String clientID){
clientFullMenu.clear();
DocumentReference docRef = mDatabase.collection("MenuDetails").document(clientID);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
String sMenuCurrency = documentSnapshot.getString("menu_currency");
colRefMenuType = docRef.collection("MenuType");
colRefMenuType.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DocumentSnapshot> menuTypeDocuments = queryDocumentSnapshots.getDocuments();
for(int i=0; i<menuTypeDocuments.size();i++){
DocumentSnapshot menuTypeDoc = menuTypeDocuments.get(i);
String sMenuType = menuTypeDoc.getId();
String sSubMenuPriority = menuTypeDoc.getString("priority");
Log.i("XXXX","document::"+menuTypeDoc.getId()+", priority::"+sSubMenuPriority);
colRefMenuItems = colRefMenuType.document(sMenuType).collection("MenuItems");
colRefMenuItems.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DocumentSnapshot> menuItemDocuments = queryDocumentSnapshots.getDocuments();
for(int j=0;j<menuItemDocuments.size();j++){
DocumentSnapshot menuItemDoc = menuItemDocuments.get(j);
String sFoodItemID = menuItemDoc.getId();
String sFoodItemPriority = menuItemDoc.getString("food_item_priority");
String sFoodItem = menuItemDoc.getString("food_item");
String sFoodItemDesc = menuItemDoc.getString("food_item_description");
String sImageUrl = menuItemDoc.getString("food_item_image_location");
String sFoodItemPrice = menuItemDoc.getString("food_item_price");
Log.i("XXXX","FoodItem::"+menuItemDoc.getId()+", priority::"+sFoodItemPriority);
boolean bMenuType = false;
FoodMenu menu = new FoodMenu();
menu.sFoodItemID = sFoodItemID;
menu.sFoodItem = sFoodItem;
menu.sFoodItemDesc = sFoodItemDesc;
menu.sFoodMenuCurrency = sMenuCurrency;
menu.iFoodItemPrice = Integer.parseInt(sFoodItemPrice);
menu.sImageUrl = sImageUrl;
menu.sMenuItemPriority = sFoodItemPriority;
//code to add the item under its corresponding food menu type (viz BREAKFAST, LUNCH, DINNER) in case it already exists
for (int i = 0; i < clientFullMenu.size(); i++) {
Log.i("MainActivity","clientFullMenu.get(i).sMenuType:" + clientFullMenu.get(i).sMenuType+" ,sMenuType:"+sMenuType);
if (clientFullMenu.get(i).sMenuType.equalsIgnoreCase(sMenuType)) {
clientFullMenu.get(i).subFoodMenu.add(menu);
bMenuType = true;
}
}
//code to add a new arraylist of food menu type (viz BREAKFAST, LUNCH, DINNER) in case it doesn't exist already
if (!bMenuType) {
ClientFullMenu clientSubMenu = new ClientFullMenu();
clientSubMenu.sMenuType = sMenuType;
clientSubMenu.sMenuPriority = sSubMenuPriority;
ArrayList<FoodMenu> subFoodMenu = new ArrayList<FoodMenu>();
subFoodMenu.add(menu);
clientSubMenu.subFoodMenu = subFoodMenu;
clientFullMenu.add(clientSubMenu);
Log.i("XXXX","Final clientFullMenu.size()1:" + clientFullMenu.size()); //it prints the correct size
}
}
}
});
}
}
});
}
}
});
Log.i("XXXX","Final clientFullMenu.size()2:" + clientFullMenu.size()); //It prints size as 0
}
public void getFoodMenu(字符串clientID){
clientFullMenu.clear();
DocumentReference docRef=mDatabase.collection(“菜单细节”).document(clientID);
docRef.get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(文档快照文档快照){
if(documentSnapshot.exists()){
String sMenuCurrency=documentSnapshot.getString(“菜单\货币”);
colRefMenuType=docRef.collection(“MenuType”);
colRefMenuType.get().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时公共无效(QuerySnapshot QueryDocumentSnapshot){
列表菜单TypeDocuments=queryDocumentSnapshots.getDocuments();
对于(int i=0;我将Log.i()
行移动到onSuccess()
中。