更新时间:2021-07-25 04:49:23
您无需使该循环同步.您真正想要的是在获得所有results
时调用completion
.您可以免费使用此解决方案:
You don't need to make this loop synchronous. What you really want is to call completion
when you will get all results
. You can use this solution (for free):
func test(strings: [String], completion: @escaping ((_ value: [String]) -> Void)) {
var results: [String] = []
for string in strings {
Service.shared.fetch(with: string, completion: { (result) in
DispatchQueue.main.async {
results.append(result)
if results.count >= strings.count {
completion(results)
}
}
)
}
}