title: my code not execute in the right order, my array is alway empty
body: <p>i'm try to run my function in order to to download some realtime database data change from firebase cloud firestore.</p>
  <p>the problem is the my closure onSuccess fire to early,  and the array in the closure is always empty.</p>
  <p>how can I handle the order of execution in order to execute the closure on success only after the forEach()</p>
  <p>after reading online I come up with some queue , but i'm not sure if i'm using correctly.</p>
  <p>below my code:</p>
  <pre><code>func getuserConfirmFriend(userLoggato: UserModel, onSuccess: @escaping([UserModel]) -&gt; Void, onError: @escaping(_ errorMessage: String) -&gt; Void, newPendingUser: @escaping(UserModel) -&gt; Void, userRemoved: @escaping(UserModel) -&gt; Void , listener: @escaping(_ listenerHandle: ListenerRegistration) -&gt; Void){
          let queue = OperationQueue()
          let listenerRegistration = db.collection(&quot;user&quot;).document(userLoggato.userID).collection(&quot;confirmFriend&quot;).addSnapshotListener(includeMetadataChanges: true) { documentSnapshot, error in
              var userConfirmFriendsArray = [UserModel]()
              guard let snapshot = documentSnapshot else { return }
              
              let operation1 = BlockOperation {
                  
                  snapshot.documentChanges.forEach { (documentChange) in
                      switch documentChange.type {
                      case .added :
                          let dict = documentChange.document.data()
                          let name = dict[&quot;name&quot;] as? String ?? &quot;na name&quot;
                          let surname = dict[&quot;surname&quot;] as? String ?? &quot;na name&quot;
                          let email = dict[&quot;email&quot;] as? String ?? &quot;na name&quot;
                          let userLevel = dict[&quot;adminLevel&quot;] as? String ?? &quot;unable to get admin level&quot;
                          let idUser = dict[&quot;userID&quot;] as? String ?? &quot;no ID&quot;
                          let position1 = dict[&quot;position&quot;] as? String ?? &quot;na preferance position&quot;
                          let position2 = dict[&quot;position2&quot;] as? String ?? &quot;na preferance position&quot;
                          let vote = dict[&quot;vote&quot;] as? Int ?? 0
                          self.downloadImageForAdmin(userID: idUser) { (urlImage) in
                              let utente = UserModel(name: name, surname: surname, email: email, userID: idUser, adminLevel: userLevel, immagine: urlImage, position: position1, position2: position2, vote: vote)
                              
                              newPendingUser(utente)
                              userConfirmFriendsArray.append(utente)
                              //                           onSuccess(userConfirmFriendsArray)
                          }
                          print(&quot;CONFIRM User Added&quot;)
                      case .modified :
                          //implements action (new escaping)
                          
                          print(&quot;CONFIRM User Modified &quot;)
                      case .removed :
                          print(&quot;CONFIRM User Removed&quot;)
                          let dict = documentChange.document.data()
                          let name = dict[&quot;name&quot;] as? String ?? &quot;na name&quot;
                          let surname = dict[&quot;surname&quot;] as? String ?? &quot;na name&quot;
                          let email = dict[&quot;email&quot;] as? String ?? &quot;na name&quot;
                          let userLevel = dict[&quot;adminLevel&quot;] as? String ?? &quot;unable to get admin level&quot;
                          let idUser = dict[&quot;userID&quot;] as? String ?? &quot;no ID&quot;
                          let position1 = dict[&quot;position&quot;] as? String ?? &quot;na preferance position&quot;
                          let position2 = dict[&quot;position2&quot;] as? String ?? &quot;na preferance position&quot;
                          let vote = dict[&quot;vote&quot;] as? Int ?? 0
                          self.downloadImageForAdmin(userID: idUser) { (urlImage) in
                              let utente = UserModel(name: name, surname: surname, email: email, userID: idUser, adminLevel: userLevel, immagine: urlImage, position: position1, position2: position2, vote: vote)
                              userRemoved(utente)
                          }
                      }
                      
                      
                      
                  }
              }
              let operation2 = BlockOperation{
                  DispatchQueue.main.async {
                      
                  onSuccess(userConfirmFriendsArray)
                  }
              }
              
              queue.addOperation(operation1)
              queue.addOperation(operation2)
              
  
          }
                
             listener(listenerRegistration)
         }
  
  </code></pre>
  <p>my problem is userConfirmFriend is always empty, even if the code execute first operation1 and then 2</p>
  <p>not so familiar with dispatch queue.</p>
  <p>thanks</p>