0

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]
  • Users
    • [userid]
      • name:"testname"
      • image:"testimage.jpg"
      • mail:"test@gmail.com"

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!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Hakan ATAK
  • 11
  • 4

2 Answers2

0

To solve this, move the declaration of your productModelList:

final List<ProductModel> productModelList = new ArrayList<>();

inside the onComplete() method, right before the starting of your for loop. The list should also be inside that method, otherwise it will be empty, due the asynchronous behaviour of the method which is called even before you are trying to add those objects to the list.

For a better understanding, I recommend you see my answer from this post and also this video.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

I edit your code. I hope this can help.

        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 (final DocumentSnapshot document : task.getResult()) {


                                DocumentReference docRef = firestore.collection("users").document(userId);
                                docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                    @Override
                                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                                        final ProductModel productModel = document.toObject(ProductModel.class);
                                        final String userId = productModel.getUserId();
                                        productModel.setUserModel(documentSnapshot.toObject(UserModel.class));
                                    }
                                });
                                productModelList.add(feedModel);
                            fireCallback.onProduct(productModelList);
                            }
                        } }});