我想加载两个 NSURLSession
。问题是在第二个 NSURLSession
中,我需要一个由第一个 NSURLSession
给出的 key 。
这就是我所做的,但它似乎不起作用!
NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)
// fetching items from json ...
for item in items {
// I extract an ID from each item
let myID = item["id"] as! String
/* Here is the problem I want to call another NSURLSession with
a url that contains this myID (String) */
let url2 = NSURL(string:"http://example.com/something?ID=\(myID)")
NSURLSession.sharedSession().dataTaskWithURL(url2!) { (data2, response2, error2) in
do {
//The problem is that this code is not executed in each for loop !
} catch let jsonError2 {
print(jsonError2)
}
}.resume()
}
} catch let jsonError {
print(jsonError)
}
}.resume()
我该怎么办?
最佳答案
对于那些感兴趣的人,解决方案是保留这段代码并添加一个对数组中的数据进行排序的函数。
关于ios - 多个 NSURLSession 加载 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834957/