title: Swift & Firestore - appending dictionary to nested array using .arrayUnion()
body: <p>I am creating a chat application where a user can start multiple chats with a different person (just like the rest of the other chat apps). I'm using <strong>Swift with Cloud Firestore</strong>.</p>
  
  <p>My database design is the following:</p>
  
  <pre><code>Chat collection: [
       "chatMember": ["member1", "member2"],
       "createdAt" : Date().timeIntervalSince1970,
       "meesages" : []
  ]
  </code></pre>
  
  <p>One chat room will have 'messages' array and it will have message <strong>dictionaries</strong> aka an object. </p>
  
  <p>Below code is where I am trying to <strong>append the message dictionary to the messages array</strong>.</p>
  
  <p>The Firebase doc introduces <code>.arrayUnion()</code> &lt;<a href="https://firebase.google.com/docs/firestore/manage-data/add-data?authuser=0#update_elements_in_an_array" rel="nofollow noreferrer">LINK</a> to the document>.
  But it gives me an error saying, </p>
  
  <p>"<em>Contextual type '[Any]' cannot be used with dictionary literal</em>"</p>
  
  <pre><code>@IBAction func sendBtnPressed(_ sender: UIButton) {
  
          if let messageBody = messageField.text, let messageSender = Auth.auth().currentUser?.email {
  
              db.collection("collectionName").document("docName").updateData([
  
  //              FieldValue.arrayUnion() does not work for this case.
  
                  K.FB.messages: FieldValue.arrayUnion([
                      "sender": messageSender,
                      "content": messageBody,
                      "createdAt": Date().timeIntervalSince1970
                  ])
              ]) { ... closure }
          }
      }   
  </code></pre>
  
  <p>I can't find any information specifically related to Swift's appending Dictionary to nested array in the Cloud Firestore. </p>
  
  <p>I found a very similar case on YouTube &lt;<a href="https://youtu.be/LKAXg2drQJQ" rel="nofollow noreferrer">https://youtu.be/LKAXg2drQJQ</a>> but this is made with Angular which uses an object with <code>{ ...curly bracket}</code>. <code>FieldValue.arrayUnion()</code> seems to work on other languages but not Swift. </p>
  
  <p>It'd be awesome if someone who has resolved this issue would help me out. </p>
  
  <p>Thank you in advance.</p>