我正在使用循环并对CLGeocoder进行API调用。 API调用正确的次数,但completionHandler只返回一次,用于第一次API调用,然后它不会为其余调用返回任何内容。API调用仅在循环中返回响应一次
dispatch_async(dispatch_get_main_queue())
{
for shop in self.shopsList {
let shopAddress = shop.address
self.geocoder.geocodeAddressString(shopAddress, completionHandler: { (placemarks, error) in
if let error = error {
print(error)
return
}
if let placemarks = placemarks where placemarks.count > 0 {
if let location = placemarks.first?.location {
print("The longitude: \(location.coordinate.longitude)")
print("The latitude: \(location.coordinate.latitude)")
}
}
})
}
self.spinner.dismiss()
self.categoryTableView.dataSource = self
self.categoryTableView.delegate = self
self.categoryTableView.reloadData()
}
其余调用根本不返回任何内容,甚至不输入任何if语句。有人能告诉我我做错了什么吗?
我曾试过把的结束for循环增加sleep(2)
,但它仍然只能返回一次
我曾尝试在for循环的末尾添加'sleep(2)',但它仍然只返回一次 – Bob
使用sleep几乎从来都不是一个好主意,特别是从主线程中。 rmaddy在他的回答中包含了关于如何执行多个地址解析请求的建议链接。至少,您需要重构代码以提交请求并等待它完成,然后再提交下一个请求。 –
使用睡眠(2)是建议我知道的建议 – Bob