Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple NSURLSession to load Json

I want to load two NSURLSession. The problem is that in the second NSURLSession I need a key which is given by the first NSURLSession.

This is what I did but it doesn't seem working !

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()

What should I do?

like image 842
Yassir Avatar asked Jan 31 '26 06:01

Yassir


1 Answers

For those who are interested, the solution was to keep this code and to add a function that sort the data in the array.

like image 101
Yassir Avatar answered Feb 02 '26 18:02

Yassir