1

I want to return an Arraylist in my doinbackroung() method of my asynctask, but i only want to do this, when i can confirm that the arraylist got populated to it's fullest. I know it is not possible to return something from an outer class in an inner class, but is there a solution to my current approach?:

@Override
        protected ArrayList<PostModel> doInBackground(Void... voids) {

            ArrayList<PostModel> posts = new ArrayList<>();
            ValidatorVariable vv = new ValidatorVariable();
            db.collection("users").document(currentuser.getUid()).collection("Friends").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    QuerySnapshot querySnapshot = task.getResult();
                    if (querySnapshot != null) {
                        List<FriendModel> userList = querySnapshot.toObjects(FriendModel.class);
                        i = 0;
                        while (i < userList.size()) {
                            FriendModel iteratedFriendModel = userList.get(i);
                            readytogo = false;
                            db.collection("users").document(iteratedFriendModel.getUserid()).collection("posts").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                    QuerySnapshot queryDocumentSnapshots = task.getResult();

                                    if (queryDocumentSnapshots != null) {
                                        List<PostModel> postsOfIteratedFriendModel = queryDocumentSnapshots.toObjects(PostModel.class);
                                        posts.addAll(postsOfIteratedFriendModel);
                                    }
                                    readytogo = true;
                                    i++;

                                }


                            });

                            while (!readytogo) {


                            }
                        }


                    }
                }
            });

Validator variable:

public class ValidatorVariable {
    private boolean valid;
    private ChangeListener listener;

    public boolean isValid(){
        return valid;
    }

    public void setValid(boolean valid){
        this.valid = valid;
        if (listener != null) listener.onChange();
    }

    public ChangeListener getListener(){
        return listener;
    }

    public void setListener(ChangeListener listener){
        this.listener = listener;
    }

    public interface ChangeListener {
        void onChange();
    }
}
Ale4303
  • 429
  • 3
  • 10
  • 29
  • You seem to be using asynchronous queries inside AsyncTask. You can solve your problem if you use Synchronous queries instead. Since you are already running this code in the background, you won't hang up the UI. – Rishabh Jain May 04 '19 at 14:17
  • @Rishabh Jain I don't think Firestore supports synchronous queries, but correct me if i'm wrong or there is another solution – Ale4303 May 04 '19 at 15:42
  • Then why do you need an AsyncTask at all? – Rishabh Jain May 06 '19 at 07:47
  • The Cloud Firestore database client, already runs all network operations in a background thread. This means that all operations take place without blocking your main thread. Putting it in an AsyncTask does not give any additional benefits. Please check the duplicates to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo May 06 '19 at 09:29

0 Answers0