1

So basically I am making an utill class so it can read my firebase database and depending on the status it will return an array list of jobs base on status.

This method work, however first time it runs it will return empty array. if I run the method again by click the function again (not re running my program) it will return the right values

public class K {

private static final String TAG = "ListingJob";
private static FirebaseFirestore mFirebaseFirestore;
private static ArrayList<Job> myListJobs = new ArrayList();//this will be returned




public static ArrayList<Job> firebaseEnquiry(Context context,String status){
    ArrayList<Applications> listApplications = new ArrayList();


    mFirebaseFirestore=FirebaseFirestore.getInstance();
    mFirebaseFirestore.collection("Applications")
            .whereEqualTo("status",status)
            .whereEqualTo("User_id", FirebaseAuth.getInstance().getCurrentUser().getUid())
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                            Applications applications=document.toObject(Applications.class);
                            listApplications.add(applications);
                        }
                        if(listApplications.size()>0){
                            //Fetch All the Jobs and compare with the pending list
                            fetchAllJobs(listApplications);

                        }
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(context, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });


    Log.d(TAG, myListJobs +"this is the myListJob" );// this is empty array 1st time running
    return myListJobs;
}


public static void fetchAllJobs(ArrayList<Applications> listApplications){
    myListJobs.clear();
    mFirebaseFirestore.collection("Jobs")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {

                            for (int i=0;i<listApplications.size();i++){
                                String testVariable = document.getId();
                                if(listApplications.get(i).getJob_id().trim().equals(testVariable.trim())){
                                    Job job=document.toObject(Job.class);
                                    myListJobs.add(job);
                                }
                            }
                        }

                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });


    }

}

Can anyone help me out ?!

Imran Khan
  • 13
  • 2

1 Answers1

0

Data is loaded from Firestore (and from most other cloud APIs) asynchronously, because it may take some time. To avoid blocking the user your main code continues while the data is being loaded, and then your onComplete gets called when the data is available.

This means that your return myListJobs runs before the onComplete ever executes, and so before you ever call myListJobs.add(job). If you run the code in a debugger and set a few breakpoints, you can most easily verify this.

The solution is always the same: any code that needs the data from Firestore, needs to be inside the callback that runs when that data is available, or be called from there through a callback.

For more on this, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807