Alamofire 调用在循环内不起作用
Alamofire call not working inside loop
我有一个循环,旨在使用 Alamofire 对 Youtube 的 API 进行 10 次不同的调用,每个调用都具有我从另一个函数获得的不同视频 ID。但是,似乎没有调用,因为我没有从 Alamofire 开关中的成功或失败选项中获得结果。
谁能指出问题所在?
func getRecom(completion: (result:String)->()) {
let array = defaults.arrayForKey("recomQuery")
for i in 0..<10 {
let videoURL = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet&id=\(array![i])&maxResults=10&key=\(apiKey)"
Alamofire.request(.GET, videoURL).validate().responseJSON { response in
switch response.result {
case .Success:
print("success") //THIS IS NOT BEING CALLED
if let value = response.result.value {
let json = JSON(value)
let videoTitle = json["items"][0]["snippet"]["title"].stringValue
print("Title from network \(videoTitle)")
let thumbPath = String(json["items"][0]["snippet"]["thumbnails"]["default"]["url"])
print(i)
let image = UIImage(data: NSData(contentsOfURL: NSURL(string: thumbPath)!)!)!
let newImage: NSData = UIImagePNGRepresentation(image)!
self.recomTitles.append(videoTitle)
self.recomThumbs.append(newImage)
}
case .Failure(let error):
print("failure") //THIS IS ALSO NOT BEING CALLED
print(error)
completion(result: "done")
}
}
}
print("saving array: \(defaults.arrayForKey("recomTitles"))")
defaults.setObject(recomThumbs, forKey: "recomThumbs")
defaults.setObject(recomTitles, forKey: "recomTitles")
completion(result: "done")
}
提前致谢!
如果 你在 Playground 中 运行 这个,程序很可能在调用打印之前结束,因为您从异步方法调用它。如果是这种情况,请在开头调用 XCPExecutionShouldContinueIndefinitely()。
谢谢大家!我认为这是一个异步调用,以便在时间之前调用完成,我通过在循环内添加来修复它:
if i == 9 { completion(result: "done") } //9 being the last index of loop
再次感谢!
我有一个循环,旨在使用 Alamofire 对 Youtube 的 API 进行 10 次不同的调用,每个调用都具有我从另一个函数获得的不同视频 ID。但是,似乎没有调用,因为我没有从 Alamofire 开关中的成功或失败选项中获得结果。
谁能指出问题所在?
func getRecom(completion: (result:String)->()) {
let array = defaults.arrayForKey("recomQuery")
for i in 0..<10 {
let videoURL = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet&id=\(array![i])&maxResults=10&key=\(apiKey)"
Alamofire.request(.GET, videoURL).validate().responseJSON { response in
switch response.result {
case .Success:
print("success") //THIS IS NOT BEING CALLED
if let value = response.result.value {
let json = JSON(value)
let videoTitle = json["items"][0]["snippet"]["title"].stringValue
print("Title from network \(videoTitle)")
let thumbPath = String(json["items"][0]["snippet"]["thumbnails"]["default"]["url"])
print(i)
let image = UIImage(data: NSData(contentsOfURL: NSURL(string: thumbPath)!)!)!
let newImage: NSData = UIImagePNGRepresentation(image)!
self.recomTitles.append(videoTitle)
self.recomThumbs.append(newImage)
}
case .Failure(let error):
print("failure") //THIS IS ALSO NOT BEING CALLED
print(error)
completion(result: "done")
}
}
}
print("saving array: \(defaults.arrayForKey("recomTitles"))")
defaults.setObject(recomThumbs, forKey: "recomThumbs")
defaults.setObject(recomTitles, forKey: "recomTitles")
completion(result: "done")
}
提前致谢!
如果 你在 Playground 中 运行 这个,程序很可能在调用打印之前结束,因为您从异步方法调用它。如果是这种情况,请在开头调用 XCPExecutionShouldContinueIndefinitely()。
谢谢大家!我认为这是一个异步调用,以便在时间之前调用完成,我通过在循环内添加来修复它:
if i == 9 { completion(result: "done") } //9 being the last index of loop
再次感谢!