I started a new project. And I'm trying to use Firebase. I try to unite the two collections and then try to combine them.
My database structure:
- Product
- [productid]
- productname:"testname"
- price:"17$"
- userid:[userid]
- [productid]
- Users
- [userid]
- name:"testname"
- image:"testimage.jpg"
- mail:"test@gmail.com"
- [userid]
My code:
public void getProductData(final FireCallback.Product fireCallback){
final List<ProductModel> productModelList = new ArrayList<>();
firestore.collection("products").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
final ProductModel productModel = document.toObject(ProductModel.class);
final String userId = productModel.getUserId();
DocumentReference docRef = firestore.collection("users").document(userId);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
productModel.setUserModel(documentSnapshot.toObject(UserModel.class));
}
});
productModelList.add(feedModel);
}
fireCallback.onProduct(productModelList);
} }});
My user model is late. And I can't set productModel.setUserModel because user data comes late. What should I do to properly fill the list.
Thanks!