0
votes

I am having trouble creating an observable that will return a list of Objects. I have a list of ids and want to make a request to my database. In this case I am using firebase. When a result is gotten i will like to compile each of these objects into a list and then return that list. I need to wait for all the objects to be returned before i return. I am doing this in my view model deserializer class. Here is my code.

private class Deserializer implements Function<QuerySnapshot, ArrayList<User>>{
    @Override
    public ArrayList<User> apply(QuerySnapshot input) {

        List<DocumentSnapshot> temp = input.getDocuments();
        final ArrayList<User> users = new ArrayList<>();
        List<String> idList = new ArrayList<>();

        for (DocumentSnapshot snapshot: temp){
            String userId= snapshot.get("userId").toString();
            idList.add(userId);
        }

        //Create observable and make request to firebase
        //compile all the returned into a list return that list
    }

}

There are several ways ways i could get data back from my firebase database, i could return the Documentsnapshot Task or i could use a call back to get my data back like so

public void getUser(String userId, final Callback callback){

    DocumentReference docRef = db.collection(DATABASE_COLLECTION_USERS).document(userId);
    docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            callback.result(documentSnapshot.toObject(User.class));
        }
    });
} 

and then add a callback when calling this function. like getUser(id, new Callback {});

The real problem i am having is how to tie all the request together. I know using RxJava is would be a solution. But i am not knowledgeable on RxJava. It is pretty similar to this question resolving a promise using mongodb and nodejs i asked earlier. That was in nodejs. But i would want to do this using RxJava

1
hello @akarnokd thanks for responding. Looking over that page, the solution close to my requirement is Parallel processing. but it does not do what i want it to. - inhaler
How about collecting all data in a list using .tolist()? - Amit Vikram Singh

1 Answers

0
votes

To anyone wandering I solved this problem by creating a cloud function that takes a list of ids and searches the database for the users and returns the list of users. This makes it so my client does not need to do any work. I used the same idea with promises from the link in my question. Thanks