我的代码未按正确的顺序执行,我的数组始终为空

我尝试运行我的功能,以便从Firebase Cloud Firestore下载一些实时数据库数据更改。

问题是我的闭包onSuccess触发得很早,并且闭包中的数组始终为空。

我如何处理执行顺序,以便仅在forEach()之后才能成功执行关闭

在线阅读后,我想出了一些队列,但不确定我是否使用正确。

在我的代码下面:

func getuserConfirmFriend(userLoggato: UserModel, onSuccess: @escaping([UserModel]) -> Void, onError: @escaping(_ errorMessage: String) -> Void, newPendingUser: @escaping(UserModel) -> Void, userRemoved: @escaping(UserModel) -> Void , listener: @escaping(_ listenerHandle: ListenerRegistration) -> Void){
        let queue = OperationQueue()
        let listenerRegistration = db.collection("user").document(userLoggato.userID).collection("confirmFriend").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["name"] as? String ?? "na name"
                        let surname = dict["surname"] as? String ?? "na name"
                        let email = dict["email"] as? String ?? "na name"
                        let userLevel = dict["adminLevel"] as? String ?? "unable to get admin level"
                        let idUser = dict["userID"] as? String ?? "no ID"
                        let position1 = dict["position"] as? String ?? "na preferance position"
                        let position2 = dict["position2"] as? String ?? "na preferance position"
                        let vote = dict["vote"] 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("CONFIRM User Added")
                    case .modified :
                        //implements action (new escaping)
                        
                        print("CONFIRM User Modified ")
                    case .removed :
                        print("CONFIRM User Removed")
                        let dict = documentChange.document.data()
                        let name = dict["name"] as? String ?? "na name"
                        let surname = dict["surname"] as? String ?? "na name"
                        let email = dict["email"] as? String ?? "na name"
                        let userLevel = dict["adminLevel"] as? String ?? "unable to get admin level"
                        let idUser = dict["userID"] as? String ?? "no ID"
                        let position1 = dict["position"] as? String ?? "na preferance position"
                        let position2 = dict["position2"] as? String ?? "na preferance position"
                        let vote = dict["vote"] 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)
       }

我的问题是,即使代码先执行operation1然后执行2,userConfirmFriend始终为空

不太熟悉调度队列。

谢谢