我正在尝试在for
循环
var loaded = false
for root in roots {
if !loaded {
let URL = NSURL(string: root + "" + attr)
let request = NSMutableURLRequest(URL: URL!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(data, response, error) -> Void in
/////////////////////// implement task ///////////////
//////// if data loaded then "loaded" becoming true //
}
task.resume()
}
}
但是在异步的情况下,这段代码执行的次数比应该多。 如何解决?