0

I have 2 examples of methods responsible for obtaining products from the database from given meals.

 private ArrayList<Product> getProductsFromDatabaseDinner() {

    dinner.getTotalProductsOfMeal().clear();
    dinner.getIdProductsOfMeal().clear();

    firebaseFirestore.collection("Users").document(user.getCurrentUserUID())
            .collection("Types of Meals").document("Dinner")
            .collection("Date of Dinner").document(date)
            .collection("List of Products")
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {

            if (task.isSuccessful()) {

                for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) {

                    dinner.getIdProductsOfMeal().add(documentSnapshot.getId());
                    dinner.getTotalProductsOfMeal().add(documentSnapshot.toObject(Product.class));
                }

                observableProductListDinner.setAll(dinner.getTotalProductsOfMeal());
            }
        }
    });

    return dinner.getTotalProductsOfMeal();
}


 private ArrayList<Product> getProductsFromDatabaseSnacks() {

    snacks.getTotalProductsOfMeal().clear();
    snacks.getIdProductsOfMeal().clear();

    firebaseFirestore.collection("Users").document(user.getCurrentUserUID())
            .collection("Types of Meals").document("Snacks")
            .collection("Date of Snacks").document(date)
            .collection("List of Products")
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {

            if (task.isSuccessful()) {

                for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) {

                    snacks.getIdProductsOfMeal().add(documentSnapshot.getId());
                    snacks.getTotalProductsOfMeal().add(documentSnapshot.toObject(Product.class));
                }

                observableProductListSnacks.setAll(snacks.getTotalProductsOfMeal());
            }

        }
    });

    return snacks.getTotalProductsOfMeal();
}

Then, after calling these methods, I would like to perform some operations associated with these products:

  Observer observer = new Observer() {
                                        @Override
                                        public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onNext(Object o) {

                                        Log.i("Test1", o.toString());
                                    }
                                };

                                Observable.create(new Observable.OnSubscribe<Object>() {
                                    @Override
                                    public void call(Subscriber<? super Object> subscriber) {

                                        subscriber.onNext(getProductsFromDatabaseDinner());
                                        subscriber.onNext(getProductsFromDatabaseSnacks());

                                        Log.i("Test2", String.valueOf(snacks.getTotalProductsOfMeal().size()));
                                    }
                                }).subscribe(observer);

Unfortunately, although the products are added to meals using the onNext method, they show me an empty ArrayList, what am I doing wrong?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Lukasso777
  • 55
  • 4

1 Answers1

0

I think what you want is:

Observable<Product> o1 = Observable
    .<List<Product>>fromCallable(() -> getProductsFromDatabaseDinner())
    .flatMap(list -> Observable.<Product>fromIterable(list));
Observable<Product> o2 = Observable
    .<List<Product>>fromCallable(() -> getProductsFromDatabaseSnacks())
    .flatMap(list -> Observable.<Product>fromIterable(list));
Observable<Product> o = o1.concatWith(o2);

Then, you can subscribe to o and get all the items.

It's also possible to chain regular RxJava operations to o (map, filter, doOnNext, etc).

JC Olivares
  • 142
  • 6