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