0

I have a problem when I am fetching my Data from Firebase Firestore. I want to get multiple Documents out of my "Posts" collection and that works fine. But to get my user documents out of my "Users" collection i need to start multiple tasks(Task) that ALL need to be completed before I want to call my callback function and i cant figure out how. Is there even a way to do it with a callback? Ive tried to solve it with Continuations but had a hard time.

Thanks in advance. Here some simple code i wrote so u can maybe understand the problem a little bit better.

    public void getPosts(final postCallback callback) {
        final FirebaseFirestore db = FirebaseFirestore.getInstance();
        CollectionReference postsRef = db.collection("Posts");
        Query postsQuery = postsRef.orderBy("createTime", Query.Direction.DESCENDING).limit(20);
        // Starting the post documents
        Task<QuerySnapshot> task = postsQuery.get();
        task.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    QuerySnapshot querySnapshot = task.getResult();
                    List<DocumentSnapshot> docsList = querySnapshot.getDocuments();
                    for(DocumentSnapshot docSnap : docsList){
                        String userID = docSnap.getString("originalPoster");
                        // getting user documents
                        Task<DocumentSnapshot> userTask = db.collection("Users").document(userID).get();
                        userTask.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                DocumentSnapshot userDoc = task.getResult();
                                String userID = userDoc.getId();
                                String firstName = userDoc.getString("first_name");
                                String surname = userDoc.getString("surname");
                                User userObject = new User(firstName, userID, surname);
                                // cant call my callback right here otherwise its called for every
                                // completed user fetch
                            }
                        });
                        // cant call my callback right here since its too early
                    }
                }else if(task.isCanceled()){
                    System.out.println("Fetch failed!");
                }
            }
        });
    }
  • Check **[this](https://stackoverflow.com/questions/48499310/how-to-return-a-documentsnapshot-as-a-result-of-a-method/48500679#48500679)** out. – Alex Mamo Jun 22 '20 at 08:20
  • I know how to work with Callbacks when there is only on asynchronous task, my question is how to ahndle an asynchronous task that receives a QuerySnapshot with multiple Documents in it and starts an asyn task for every single one of them. e.g. if you get multiple post documents as a result of your first task and u want to fetch the user who posted them as well. – HSchlong Jun 22 '20 at 16:38

0 Answers0