title: Firebase Firestore Messing Up Order Of Array
body: <p>I'm using Firebase's Firestore to store data for my iOS app. In Firestore, I have two collections. One called <code>songs</code> and one called <code>playlists</code>. The <code>songs</code> collection contain many documents and each document has a single song info inside of it. Here's what a <code>songs</code> document looks like.</p>
  
  <p><a href="https://i.stack.imgur.com/izlBc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/izlBc.png" alt="songs documet"></a></p>
  
  <p>Then, in my collection <code>playlists</code> it contains some documents which are playlists. Here's an example of a <code>playlists</code> document.</p>
  
  <p><a href="https://i.stack.imgur.com/p1lRw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p1lRw.png" alt="playlists document"></a></p>
  
  <p>I want to be able to display a playlist, and then get the songTitles from that playlist from my <code>songs</code> collection. So, I've done this:</p>
  
  <pre><code>var testPlaylistTitles = [String]()
  db.collection("playlists").whereField("title", isEqualTo: "Test Playlist").getDocuments { (snapshot, error) in
  
         for document in (snapshot?.documents)! {
             self.testPlaylistTitles = ((document.data()["songTitles"] as? [String])!)
  
                  }
            }
  </code></pre>
  
  <p>This makes the variable <code>testPlaylistTitles</code> = <code>["Test Song", "Test Song 2", "Test Song 3"]</code>. And that's great. But this is where it goes wrong. I want to get information about each one of these songs from my <code>songs</code> collection. So, I create a for loop and loop through the songs to append to my other arrays of artist, images, and titles. Just for this example, I'll use a variable of <code>returedTitles</code>.</p>
  
  <pre><code>var testPlaylistTitles = [String]()
  db.collection("playlists").whereField("title", isEqualTo: "Test Playlist").getDocuments { (snapshot, error) in
  
         for document in (snapshot?.documents)! {
             testPlaylistTitles = ((document.data()["songTitles"] as? [String])!)
  
                  }
         var returedTitles = [String]()
         for i in 0...testPlaylistTitles.count-1 {
         db.collection("songs").whereField("title", isEqualTo: testPlaylistTitles[i]).getDocuments(completion: { (snapshot, error) in
                      for document in (snapshot?.documents)! {
                          returedTitles.append((document.data()["title"] as? String)!)
  
                      }
                  })
             } 
     }
  </code></pre>
  
  <p>Now, <code>returedTitles</code> is equal to a shuffled version of the original <code>testPlaylistTitles</code>. I thought at first that somehow it was ordering <code>testPlaylistTitles</code> by alphabetical order, but it isn't. Does anyone have any ideas on what is happening and how to fix it! Thanks a lot! This has been stumping me for a while.</p>