1

I have a question about some functionalities of Firestore. I am trying to occupy an array of data obtained through multiple read operations from the Firestore database. Is there a way for me to be notified when all the data are successfully read and stored in my array? This is particularly an issue because read operations are not finished in the order that they are called. Here are some code that illustrates my problem:

/* My array to insert the data read from the Firestore database */
String[] my_array = new String[3];

/* A method that will be called to initialize our array */
private void initArray(String doc_one, String doc_two, String doc_three) {
    initSingleIndex(0, doc_one);
    initSingleIndex(1, doc_two);
    initSingleIndex(2, doc_three);
}

private void initSingleIndex(final int index, String doc_id) {
    /* We perform our read operation here */
    question_ref.document(doc_id).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            my_array[index] = documentSnapshot.getString("some_field");
        }
    });
}

My current implementation is to keep a global counter, which will be used to keep track of how many read operations were successfully carried out. I am also wondering whether the onSuccess() callbacks can be fired concurrently, since this will then lead to data corruption (i.e. the classic problem of incrementing values concurrently).

Any help or suggestion will be appreciated.

Apper
  • 311
  • 1
  • 3
  • 10
  • Do you have a data structure of your current implementation? – Chris Chen Jan 30 '18 at 05:23
  • @ChrisChen I'm just using the good-old array of Java. Basically, I just insert three results obtained through read operations into index 0, 1 and 2 of the array. The reason why I chose an array for this is because I know before-hand that all I need is to retrieve exactly 3 separate data. Does this answer your question? – Apper Jan 30 '18 at 05:29
  • Please show some code that illustrates what you're dealing with. Platform is important. – Doug Stevenson Jan 30 '18 at 05:30
  • @DougStevenson Right, sorry for that. I'm new here on StackOverflow, and will do my best to follow the best-practises. I'll edit my question and add some platform specific tags. – Apper Jan 30 '18 at 05:32
  • No, what I mean is the data struction in your Firestore. But have you check https://firebase.google.com/docs/firestore/data-model#references to reference each document for your read event? – Chris Chen Jan 30 '18 at 05:39
  • Please take a look at my answer given yesterday in this [post](https://stackoverflow.com/questions/48499310/firestore-object-with-inner-object). – Alex Mamo Jan 30 '18 at 08:30

0 Answers0