0

我已经检查了有关此主题的所有问题,但没有一个有帮助。我有如下代码。现在,只要代码进入for id in stringArray循环,它就会在stringArray.count没有进入 firestore 异步部分的情况下执行一段时间。这是可以理解的,但不是我的要求。我想myGroup.notify在循环和异步代码完成后执行。但是由于循环很容易完成,因此myGroup.notify即使异步尚未完成也会被调用。所以,基本上可能是我在这里要求将myGroup.enterand放置myGroup.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")
        }
   }
4

1 回答 1

1

离开异步块而不是它之外......并且输入应该等于离开所以把它放在 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")
         }
    }
于 2020-07-25T19:43:52.200 回答