Swift 如果执行嵌套异步调用,函数将运行两次,否则将运行一次。需要帮助预先确定何时会发生这种情况
func-handleGetAllPhotoURLs从下面的一行调用,我已经确认代码行只在有断点的情况下执行一次Swift 如果执行嵌套异步调用,函数将运行两次,否则将运行一次。需要帮助预先确定何时会发生这种情况,swift,Swift,func-handleGetAllPhotoURLs从下面的一行调用,我已经确认代码行只在有断点的情况下执行一次 _ = FlickrClient.getAllPhotoURLs(currentPin: self.currentPin, fetchCount: fetchCount, completion: self.handleGetAllPhotoURLs(pin:urls:error:)) 根据我的print语句的输出,该函数运行两次,因为如果url.count非零,它将打印两行输出。但
_ = FlickrClient.getAllPhotoURLs(currentPin: self.currentPin, fetchCount: fetchCount, completion: self.handleGetAllPhotoURLs(pin:urls:error:))
根据我的print语句的输出,该函数运行两次,因为如果url.count非零,它将打印两行输出。但是,如果urls.count为零,那么我只得到一条声明“urls.count-->0”的打印语句
handleGetAllPhotoURLs-->urls.count-->0//始终打印此行
handleGetAllPhotoURLs-->urls.count-->21//仅当URL参数不为空时才打印此行
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?){
print("handleGetAllPhotoURLs ---> urls.count ---> \(urls.count)")
let backgroundContext: NSManagedObjectContext! = dataController.backGroundContext
if let error = error {
print("func mapView(_ mapView: MKMapView, didSelect... \n\(error)")
return
}
let pinId = pin.objectID
backgroundContext.perform {
let backgroundPin = backgroundContext.object(with: pinId) as! Pin
backgroundPin.urlCount = Int32(urls.count)
try? backgroundContext.save()
}
for (index, currentURL) in urls.enumerated() {
URLSession.shared.dataTask(with: currentURL, completionHandler: { (imageData, response, error) in
guard let imageData = imageData else {return}
connectPhotoAndPin(dataController: self.dataController, currentPin: pin , data: imageData, urlString: currentURL.absoluteString, index: index)
}).resume()
}
}
此外,我有一个UILabel,它只在URL.count为零时显示自己,我只想在URL为空时显示它
现在,如果URL不是空的,应用程序会很快刷新空消息标签。这对我来说很有意义,因为print语句显示URL数组暂时为空
当URL.count为非零时,是否有办法避免向用户闪烁空消息UILabel
编辑:根据请求在下面添加代码。调用下面的函数以在完成处理程序中获取[URL]。然后将完成处理程序输入到:
func handleGetAllPhotoURLs(pin:pin,URL:[URL],错误:错误?
在
do
块中,调用completion
两次。请看更正
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
if temp.photos.photo.isEmpty == false {
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
} else {
completion(currentPin, [], nil)
}
return
}
函数的内容不影响函数被调用的次数。我们必须看看你是如何调用这个方法的。进行编辑,希望能提供更多信息。类func getAllPhotoURLs()返回一个完成处理程序,该处理程序随后进入func handleGetAllPhotoURLs()。如果还有什么我能帮忙的,请告诉我。非常感谢。我找错地方了。我不知道我回来了两次。
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
if temp.photos.photo.isEmpty == false {
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
} else {
completion(currentPin, [], nil)
}
return
}