小编Yas*_*sir的帖子

NSURLSession任务在for循环中运行

我正在尝试使用an NSURLSession在for循环中加载多个请求.

for id in ids{
    // ids is an Array of String
    let url = NSURL(string:"http://example.com/something?ID=\(id)")   
    //                                                     ^ 
    NSURLSession.sharedSession().dataTaskWithURL(url!){(data, response, error)in
        if error2 != nil {
            print(error2)
            return
        }
        do{
            let strjson = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)

            // Here is the problem the for loop doesn't let enough time to the NSURLSession
        }catch let errorjson {
            print(errorjson)
        }
    }.resume 
}
Run Code Online (Sandbox Code Playgroud)

ios nsurlsession swift

1
推荐指数
1
解决办法
585
查看次数

标签 统计

ios ×1

nsurlsession ×1

swift ×1