如何在异步函数 Swift 的循环中使用 Dispatch Group?

How to use Dispatch Group in a loop with asynchronous Function Swift?

我已经检查了有关此主题的所有问题,但其中 none 个问题有所帮助。 我有如下代码。现在,一旦代码进入 for id in stringArray 循环,它就会执行 stringArray.count 次,甚至没有进入 firestore 异步部分。这是可以理解的,但不是我的要求。我希望 myGroup.notify 在循环和异步代码完成后执行。但是由于循环很容易完成,所以 myGroup.notify 即使在异步尚未完成时也会被调用。所以,基本上可能是我在这里要求将 myGroup.entermyGroup.leave 放在正确的位置。谢谢!

我的代码:

func Promise_searchedDataFromDB(stringArray:[String]) {
    for id in stringArray {
        myGroup.enter()
    collectionRef.getDocuments { (querySnapshot, error) in
        if error != nil {
            return
        }
        else {
               guard let snapshot = querySnapshot else {return}
               for document in snapshot.documents {
                
                let myData = document.data()
                if StaticVariable == true {
             
                    self.typeOfListing = myData["Type"] as? String ?? "Not Found"
                    self.charges = Int(myData["Charges"] as? String ?? "Not Found") ?? 0
                    self.nameOfListing = myData["Title"] as? String ?? "Not Found"
                    self.currency = myData["Currency"] as? String ?? "Not Found"
                    self.days = myData["Days"] as? String ?? "Not Found"
                    self.details = myData["Description"] as? String ?? "Not Found"
                    self.cityName = myData["City"] as? String ?? "Ghost"
                    
                    let dataArray = CellComponents(image: UIImage(named: "b")!, typeOfListing: self.typeOfListing , charges: self.charges, rating: 4.1, nameOfListing: self.nameOfListing , cityName: self.cityName, detail: self.details, currency: self.currency, days: self.days)
                    self.ArrayToHoldSearchedListing.append(dataArray)
                    self.tableView.reloadData()
                }
              }
            }
          }

             myGroup.leave()
       }
        
        myGroup.notify(queue: .main) {
            print("All done")
        }
   }

在异步块内离开,而不是在异步块外...并且进入应该等于离开,所以将它放在 defer 语句中以始终在离开范围之前执行

  func Promise_searchedDataFromDB(stringArray:[String]) {
     for id in stringArray {
         myGroup.enter()
     collectionRef.getDocuments { (querySnapshot, error) in
        defer{  myGroup.leave() }
        
         if error != nil {
             return
         }
         else {
                guard let snapshot = querySnapshot else {return}
                for document in snapshot.documents {
                 
                 let myData = document.data()
                 if StaticVariable == true {
              
                     self.typeOfListing = myData["Type"] as? String ?? "Not Found"
                     self.charges = Int(myData["Charges"] as? String ?? "Not Found") ?? 0
                     self.nameOfListing = myData["Title"] as? String ?? "Not Found"
                     self.currency = myData["Currency"] as? String ?? "Not Found"
                     self.days = myData["Days"] as? String ?? "Not Found"
                     self.details = myData["Description"] as? String ?? "Not Found"
                     self.cityName = myData["City"] as? String ?? "Ghost"
                     
                     let dataArray = CellComponents(image: UIImage(named: "b")!, typeOfListing: self.typeOfListing , charges: self.charges, rating: 4.1, nameOfListing: self.nameOfListing , cityName: self.cityName, detail: self.details, currency: self.currency, days: self.days)
                     self.ArrayToHoldSearchedListing.append(dataArray)
                     self.tableView.reloadData()
                 }
               }
             }
           }

            
        }
         
         myGroup.notify(queue: .main) {
             print("All done")
         }
    }