In our app we are trying to fetch all the products and by iterating each product trying to get specificationId
which is equal to another documentId
in seperate collection Specifications.
Basically the structure is like below:
ProductDetail
- productId
- productTitle
- productDescription
- specificationId
Specification
- documentId — this is autogenerated id for collection which is being used as
specificationId
inProductDetail
collection.
MainActivity.java
Query productResponseQuery = FirebaseFirestore.getInstance()
.collection("productdetails")
.limit(50);
productResponseQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onEvent(@Nullable QuerySnapshot querySnapshot, @Nullable FirebaseFirestoreException error) {
if (error != null) {
Log.d(TAG, "Error getting documents: " + error.getMessage());
return;
}
querySnapshot.forEach(doc -> {
ProductDetailResponse productDetailResponse = new ProductDetailResponse();
productDetailResponse.setProductImage(doc.getData().get("productImageUrl").toString());
productDetailResponse.setProductTitle(doc.getData().get("productTitle").toString());
productDetailResponse.setProductShortDesc(doc.getData().get("productShortDesc").toString());
productDetailResponse.setProductDesc(doc.getData().get("productDescription").toString());
populateSpecification(doc.getData().get("specId").toString());
});
}
});
private void populateSpecification(String specId){
DocumentReference specDocumentRef = FirebaseFirestore.getInstance()
.collection("specifications")
.document(specId);
specDocumentRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.getResult().exists()){
task.getResult();
}else{
task.getException();
}
}
});
}
I understand that the Firestore database calls are asynchronous in nature. Hence if I call the specificationQuery inside the for loop, without waiting for the result it is jumping to the next iteration.
Is there any way without changing the structure I can achieve the result, which will basically show all the product details along with their respective specification collection?