title: Thread 1: EXC_BAD_INSTRUCTION when fetching data
body: <p>I get this Error -&gt; <code>Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)</code> randomly. I don't quite understand when exactly it happens. Most of the times it is when the view refreshes. The Error appears at the line where <code>group.leave()</code> gets executed.</p>
  <p>What am I trying to do:
  I want to fetch albums with their image, name and songs that also have a name and image from my firebase database. I checked for the values and they're all right as far as I can tell. But when trying to show them it is random what shows. Sometimes everything is right, sometimes one album gets showed twice, sometimes only one album gets showed at all, sometimes one album has the songs of the other album.</p>
  <p>My firebase database has albums stored as documents, each document has albumimage/name and 2 subcollections of &quot;unlocked&quot; with documents(user uid) that store &quot;locked&quot;:Bool and &quot;songs&quot; with a document for each song that stores image/name
  <a href="https://i.stack.imgur.com/toTfX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/toTfX.png" alt="enter image description here" /></a></p>
  <p>This is the function that fetches my albums with their songs:</p>
  <pre><code>    let group = DispatchGroup()
      @State var albums: [Album] = []
      @State var albumSongs: [AlbumSong] = []
  
  func fetchAlbums() {
  FirebaseManager.shared.firestore.collection(&quot;albums&quot;).getDocuments { querySnapshot, error in
              if let error = error {
                  print(error.localizedDescription)
                  return
              }
              guard let documents = querySnapshot?.documents else {
                  return
              }
              let uid = FirebaseManager.shared.auth.currentUser?.uid
              
              documents.forEach {  document in
                  let data = document.data()
                  let name = data[&quot;name&quot;] as? String ?? &quot;&quot;
                  let artist = data[&quot;artist&quot;] as? String ?? &quot;&quot;
                  let releaseDate = data[&quot;releaseDate&quot;] as? Date ?? Date()
                  let price = data[&quot;price&quot;] as? Int ?? 0
                  let albumImageUrl = data[&quot;albumImageUrl&quot;] as? String ?? &quot;&quot;
                  let docID = document.documentID
                  FirebaseManager.shared.firestore.collection(&quot;albums&quot;).document(docID)
  .collection(&quot;songs&quot;).getDocuments { querySnapshot, error in
                      if let error = error {
                          return
                      }
                      guard let documents = querySnapshot?.documents else {
                          return
                      }
                      self.albumSongs = documents.compactMap { document -&gt; AlbumSong? in
                          do {
                              return try document.data(as: AlbumSong.self)
                          } catch {
                              return nil
                          }
                      }
                      group.leave()
                  }
                  FirebaseManager.shared.firestore.collection(&quot;albums&quot;).document(docID)
  .collection(&quot;unlocked&quot;).document(uid ?? &quot;&quot;).getDocument { docSnapshot, error in
                      if let error = error {
                          return
                      }
                      guard let document = docSnapshot?.data() else {
                          return
                      }
                      group.enter()
                          group.notify(queue: DispatchQueue.global()) {
                              if document[&quot;locked&quot;] as! Bool == true {
                          self.albums.append(Album(name: name, artist: artist, 
  songs: albumSongs, releaseDate: releaseDate, price: price, albumImageUrl: albumImageUrl))
                                  print(&quot;albums: &quot;,albums)
                              }
                          }
                  }
              }
          }
  }
  </code></pre>
  <p>I call my <code>fetchAlbums()</code> in my view <code>.onAppear()</code></p>
  <p>My AlbumSong:</p>
  <pre><code>struct AlbumSong: Identifiable, Codable {
      @DocumentID var id: String? = UUID().uuidString
      let title: String
      let duration: TimeInterval
      var image: String
      let artist: String
      let track: String
      }
  </code></pre>
  <p>My Album:</p>
  <pre><code>struct Album: Identifiable, Codable {
      @DocumentID var id: String? = UUID().uuidString
      let name: String
      let artist: String
      let songs: [AlbumSong]
      let releaseDate: Date
      let price: Int
      let albumImageUrl: String
    }
  </code></pre>
  <p>I tried looking into how to fetch data from firebase with async function but I couldn't get my code to work and using dispatchGroup worked fine when I only have one album. I would appreciate answers explaining how this code would work with async, I really tried my best figuring it out by myself a long time. Also I would love to know what exactly is happening with DispatchGroup and why it works fine having one album but not with multiple ones.</p>