public void showTotalFreshness(String userID){
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference bigIngredientRef = db.collection("사용자").document(userID)
.collection("냉동실"); //대분류 Ref
bigIngredientRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
final long[] addAllDate = {0};
final int[] num = {0};
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.exists()) {
String bigIngredientName = document.getId();
CollectionReference smallIngredientRef = bigIngredientRef.document(bigIngredientName)
.collection(bigIngredientName);
smallIngredientRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(QueryDocumentSnapshot document : task.getResult()) {
long totalEd = document.getLong("유통기한");
addAllDate[0] += totalED; //this is the add code
num[0]++;
}
}
});
}
}
Log.d("HELLO", addAllDate[0] + " " + num[0]); //it comes out 0
}
I want to add all addAllDate and num inside the addOncompleteListener. But it comes out the first value(0). How can I get all the sum Of values? I want to know the answer!!!