0

I am using Firestore by Firebase. There I am using the ArrayList. I save a lot of numbers in a few ArrayLists. But now I have the problem, that I can not save a number twice in an ArrayList.
Then I found this where it is described(docs): https://firebase.google.com/docs/firestore/manage-data/add-data
"arrayUnion() adds elements to an array but only elements not already present"

Is there really no way to save a number twice or more times in an ArrayList in Firestore?

But an interesting thing is that I can add the values at my dashboard, but the user through code not.

EDIT:
I am adding the numbers like this to the ArrayList:

gradesRef.onSnapshot((doc) => {
          gradesRef.update({
            [grade_type]: firebase.firestore.FieldValue.arrayUnion(grade)
          });
        });
Filip Degenhart
  • 631
  • 4
  • 19

2 Answers2

2

In JSON and in most programming languages an array can contain the same value multiple times.

But many developers use arrays to implement mathematical sets, which are a collection of unique values. That means that in most programming languages you end up with this operation in many places. In JavaScript for example:

if (!array.contains("my new value")) {
  array.push("my new value");
}

You now have two statements to accomplish one operation, which leads to a race condition (especially in multi-user scenarios).

For this reason Cloud Firestore added a atomic operation array-union, which adds an item to an array if it isn't in there already. You should use that operation if you want to have unique values in your array.

If you don't want the values in your array to be unique, you should use the normal array operators of your programming language. In JavaScript this would simply be the push() operation we used above, but without the if around it:

array.push("my new value");

Update based on your code sample.

gradesRef.get().then((doc) => {
    let grades = doc.data()[grade_type] || [];
    grades.push(grade);
    gradesRef.update({
        [grade_type]: grades
    });
});

So:

  1. Get the existing grades array from the document
  2. Add the new item to the end of the array
  3. Write the updated grades back to the database.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can you please look at my edit and say me how to rewrite it that it works? – Filip Degenhart Feb 11 '19 at 17:59
  • You're still using `arrayUnion`. I updated my answer with a sample. – Frank van Puffelen Feb 11 '19 at 18:36
  • When With this code I get the following problem: My ArrayList has only one number. Number 8. Then I wanted to add again number 8 with your code. I expected that in the ArrayList there should be two times the 8. BUT there where to many eights. See here: https://www.dropbox.com/s/awx6dg1fjl38u5a/tt.PNG?dl=0 – Filip Degenhart Feb 11 '19 at 20:44
  • Ah yes, that's because you're using `onSnapshot`. So every time you update the document, it triggers the same `onSnapshot`, which causes another update. You probably want to use `get()`. Note that such things are easiest to troubleshoot if you run the code in a debugger and check what's going on. – Frank van Puffelen Feb 11 '19 at 20:53
0

Why would you want to add the same data twice?

If it's really different data that happens to have the same value, consider creating an object that contains a unique id and the value, and add a new object containing the duplicate value with a new id.

jvdmr
  • 685
  • 5
  • 12