Reputation: 129
I create a model, get a document from the Firebase firestore document, and store it in the model.
In the model there is a list of another model, and this another model has a list of third model (basically the first model or document has sub collection that I want to store it inside the model).
I used for loop to store every document inside the sub-collections. Its working fine, all data is stored, but the problem is the function that I write after the for loop, is being called before the data is stored.
I checked it, it call the function inside the loops and its working, but when I call it after the loop its being called before the loops done. I even set a button to call that function, its working just need time to store the data.
MyModels currentSelectedModels;
myDocumentRef.document(date).get().addOnSuccessListener(documentSnapshot -> {
currentSelectedModels = documentSnapshot.toObject(MyModels.class);
documentSnapshot.getReference().collection("product").get().addOnSuccessListener(queryDocumentSnapshots1 -> {
for(DocumentSnapshot documentSnapshot1 : queryDocumentSnapshots1){
SecondModels SecondModels = documentSnapshot1.toObject(SecondModels.class);
documentSnapshot1.getReference().collection("subproduct").get().addOnSuccessListener(queryDocumentSnapshots2 -> {
for(DocumentSnapshot documentSnapshot2 : queryDocumentSnapshots2){
ThirdModels ThirdModels = documentSnapshot2.toObject(ThirdModels.class);
SecondModels.getSubproductList().add(ThirdModels);
}
currentSelectedModels.getproductList().add(SecondModels);
// Working, but since its inside the loop, its going to be called every time there is a document
// I tested it with 1 document and its working fine
//Toast.makeText(MyFragment.this.getContext(), "" + currentSelectedModels.getDetails(), Toast.LENGTH_SHORT).show();
//Toast.makeText(MyFragment.this.getContext(), "" + currentSelectedModels.getproductList().size(), Toast.LENGTH_SHORT).show();
setData();
});
}
// Didnt Work
Toast.makeText(MyFragment.this.getContext(), "" + currentSelectedModels.getDetails(),
Toast.LENGTH_SHORT).show();
Toast.makeText(MyFragment.this.getContext(), "" + currentSelectedModels.getproductList().size(),
Toast.LENGTH_SHORT).show();
setData();
});
}).addOnCompleteListener(task -> {
if(task.isComplete()){
if (task.isSuccessful()) {
//didnt work
//setData();
}
}
});
The second toast in the code which the uncommented one, its being called after the loop, and the first models has the data but the second which being stored by the loop says 0.
Upvotes: 1
Views: 280
Reputation: 129
i've found a way, maybe not the best way, if anyone have anything better than this, lemme now.
i added this inside the first for loop
if(queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1).getId() == documentSnapshot.getId()){
setData();
}
basically, it check if the current document id is the last document id of the querysnapshots
apparently when i add another document in the database, the querysnapshots for loop is end with the first 1 wich is index 0
if(queryDocumentSnapshots.getDocuments().get(0).getId() == documentSnapshot.getId()){
setData();
}
i was wrong, the query snapshot diddnt start from 0 or from the end, idk from where because its random, so i've come up with the final solution
if(currentSelectedModels.getProductList().size() == queryDocumentSnapshots.size()){
setData();
}
so it check if the amount of product document is the same amount of the my current model product list
Upvotes: 1