Ios 使用异步调用for循环处理完成

Ios 使用异步调用for循环处理完成,ios,swift,closures,completionhandler,Ios,Swift,Closures,Completionhandler,我有一个函数,它应该有一个完成处理程序,只有当函数中的所有内容都实际完成时才应该调用它。这就是我的职责: static func getWishes(dataSourceArray: [Wishlist], completion: @escaping (_ success: Bool, _ dataArray: [Wishlist]) -> Void){ var dataSourceArrayWithWishes = dataSourceArray le

我有一个函数,它应该有一个
完成处理程序
,只有当函数中的所有内容都实际完成时才应该调用它。这就是我的职责:

static func getWishes(dataSourceArray: [Wishlist], completion: @escaping (_ success: Bool, _ dataArray: [Wishlist]) -> Void){
    
    var dataSourceArrayWithWishes = dataSourceArray
    
    let db = Firestore.firestore()
    let userID = Auth.auth().currentUser!.uid
    
    for list in dataSourceArray {
        db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").order(by: "wishCounter").getDocuments() { ( querySnapshot, error) in
            if let error = error {
                print(error.localizedDescription)
                completion(false, dataSourceArrayWithWishes)
            } else {
                // append every Wish to array at wishIDX
                for document in querySnapshot!.documents {
                    let documentData = document.data()
                    let imageUrlString = document["imageUrl"] as? String ?? ""
                    
                    let imageView = UIImageView()
                    imageView.image = UIImage()
                    if let imageUrl = URL(string: imageUrlString) {
                        let resource = ImageResource(downloadURL: imageUrl)
                        imageView.kf.setImage(with: resource) { (result) in
                            switch result {
                            case .success(_):
                                dataSourceArrayWithWishes[wishIDX].wishes.append(Wish(name: name, link: link, price: price, note: note, image: imageView.image!, checkedStatus: false))
                                completion(true, dataSourceArrayWithWishes)
                                print("success")
                            case .failure(_):
                                print("fail")
                            }
                        }
                    } else {
                       dataSourceArrayWithWishes[wishIDX].wishes.append(Wish(name: name, link: link, price: price, note: note, image: imageView.image!, checkedStatus: false))
                    } 
                }
            }
        }
    }
}
问题在于
imageView.kf.setImage…

现在我在第一次
成功后调用
completion
,但是只有当
for循环
和所有
设置图像
都完成时,该函数才应该完成。我已经试过几次了,但都没法成功。因此,我想知道对于这种情况,最佳实践是什么?

以下是您如何使用
DispatchGroup
来解决这个问题。。。您需要
DispatchGroup
asynchronous
循环完成时获得通知

 static func getWishes(dataSourceArray: [Wishlist], completion: @escaping (_ success: Bool, _ dataArray: [Wishlist]) -> Void){
       
       var dataSourceArrayWithWishes = dataSourceArray
       
       let db = Firestore.firestore()
       let userID = Auth.auth().currentUser!.uid
        let group = DispatchGroup()
    
       for list in dataSourceArray {
        
        group.enter()
           db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").order(by: "wishCounter").getDocuments() { ( querySnapshot, error) in
            defer{ group.leave() }
               if let error = error {
                   print(error.localizedDescription)
                   completion(false, dataSourceArrayWithWishes)
               } else {
                   // append every Wish to array at wishIDX
                   for document in querySnapshot!.documents {
                    group.enter()
                       let documentData = document.data()
                       let imageUrlString = document["imageUrl"] as? String ?? ""
                       
                       let imageView = UIImageView()
                       imageView.image = UIImage()
                       if let imageUrl = URL(string: imageUrlString) {
                           let resource = ImageResource(downloadURL: imageUrl)
                           imageView.kf.setImage(with: resource) { (result) in
                            defer{ group.leave() }
                               switch result {
                               case .success(_):
                                   dataSourceArrayWithWishes[wishIDX].wishes.append(Wish(name: name, link: link, price: price, note: note, image: imageView.image!, checkedStatus: false))
                                   
                               case .failure(_):
                                   print("fail")
                               }
                           }
                       } else {
                          dataSourceArrayWithWishes[wishIDX].wishes.append(Wish(name: name, link: link, price: price, note: note, image: imageView.image!, checkedStatus: false))
                       }
                   }
               }
           }
       }
    
    group.notify(queue: DispatchQueue.main) {
        completion(true, dataSourceArrayWithWishes)
        print("success")
    }
   }

相关:@vadian正是我需要的!我找的时候什么也没找到,谢谢你!