2

I use firebase firestore to store some data. I store the data at the TimeForm activity.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
globalClass.setAction(actionText);
globalClass.setTime(totalTime);
globalClass.setUser_email(user.getEmail());
ScheduleClass scheduleClass = new ScheduleClass(fromEditText.getText().toString() + " - " + untilEditText.getText().toString(), multiLineEditText.getText().toString(), user.getEmail());
Map<String, Object> userMap = new HashMap<>();
userMap.put("Time", globalClass.getTime());
userMap.put("Action", globalClass.getAction());
userMap.put("user_email", globalClass.getUser_email());
mondayCollectionReference = db.collection("Monday");
tuesdayCollectionReference = db.collection("Tuesday");                                                      
mondayCollectionReference.document().set(userMap, SetOptions.merge()).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(TimeForm.this, "Submitted", Toast.LENGTH_SHORT).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d("ERROR", e.getMessage());
        }
    });

Everything works perfectly here, the data are stored with the format I wanted to. Then I retrieve the data at MainActivity. So in TimeForm the user adds time like 15:00 - 17:00 but he adds multiple, and in MainActivity I store all of the times at an ArrayList. Here is the code:

db = FirebaseFirestore.getInstance();
final ArrayList<String> loadMondayTimeArrayList = new ArrayList<>();
user = FirebaseAuth.getInstance().getCurrentUser();
db.collection("Monday").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
        for(DocumentSnapshot documentSnapshot : documentSnapshots){
            String time;
            String action;
            String user_email;
            time = documentSnapshot.getString("Time");
            action = documentSnapshot.getString("Action");
            user_email = documentSnapshot.getString("user_email");
            mondayTimeArrayList.add(time);
        }
    }
});

When I log the time inside the addSnapShotListener it logs the time perfectly. But when I add the time to mondayTimeArrayList and try to Log the items of mondayTimeArrayList outside of the addSnapShotListener I get an Index Out Of Bounds error.

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

1 Answers1

1

That's because adding a snapshot listener is asynchronous, and the method returns immediately. Your array won't be populated until whenever the callback is invoked and finishes successfully. If you want to use the results of your listener, you will only be able to access them after the listener is fully complete.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • The callback will end, but other code after you pass the callback will keep running. That's how asynchronous code works. – Doug Stevenson Sep 06 '18 at 19:21
  • Right, and how can I make it stop, or is there any other way I can get those strings outside the addSnapshotListener ? –  Sep 06 '18 at 19:22