0

I'm making a scheduling app, and storing all the scheduled things in firebase with arrays. When I try to schedule something with the same string value, it fails and doesn't add it to the array. I don't know if this is something in swift I can edit, or if it's a firebase setting.

If it's something in swift, here's the code updating the array:

doc.updateData([
"Instructor": FieldValue.arrayUnion(["\(scheduleinstructor)"])
])

If it's something in firebase, could someone please explain a way around this or a simple fix I overlooked?

1 Answer 1

1

According to the documentation on adding items to an array:

arrayUnion() adds elements to an array but only elements not already present

So the fact that the duplicate entry is not added is by design. If you want to allow that, you'll have to:

  1. Read the document with the array from the databae.
  2. Extract the array from the document into your application code.
  3. Add the item to the array.
  4. Write the entire modified array back to the database.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.