-1

I don't know how to put it correctly so I will show in pictures.

I create first URLSession, fetch data from JSON, including links to data I want to use in the same Model.

first link:

SchreenShot

From first request I got links to other info I want to use in the same table:

links from first link:

ScreenShot

I'm trying to fetch data from both first and second link and put it together in one data model/table/cell.

I need to make another URLSession, but I can't understood how put info together. Is it possible to fire one URLSession inside another? Any suggestions how to make it?

muescha
  • 1,544
  • 2
  • 12
  • 22
odvan
  • 59
  • 2
  • 8
  • please add your code also as text. select the code and press the button `{}` then it gets an indention of 4 spaces and is displayed as code. (you can leave the images) – muescha Feb 02 '17 at 00:05
  • ok - json data is free on the api homepage – muescha Feb 02 '17 at 00:41
  • Send a notice to delete this question since I want describe problem with code. – odvan Feb 02 '17 at 14:49

3 Answers3

0

I would recommend using some libraries to abstract some complexity - Alamofire/SwiftyJSON

Alamofire.request(url, method: .get).responseJSON { response in
switch response.result {
case .success(let value):
    let json = JSON(value)
    //pull your links out of the json and make another request here
    Alamofire.request(json["fixtures"]["competition"]["href"], method: .get).responseJSON { response in
       switch response.result {
          case .success(let value): 
          let json2 = JSON(value)
          //handle the json from your second request
        }
   }
}

Keep in mind that this is pseudo code. Don't try to cut and paste - but it should get you in the general direction.

dmorrow
  • 5,152
  • 5
  • 20
  • 31
  • Its something i thought about, put second URLSession into another. But app crashed with findind null unwrapping some variable. – odvan Feb 02 '17 at 02:04
  • SwiftyJSON will help with parsing JSON and safely unwrapping null. But, without your actual code, its impossible to help you out more. – dmorrow Feb 02 '17 at 12:16
0

create a job queue where you place your task (=Retrieve Data) into.

start the queue with your root url call. when it comes back from async call, parse the JSON data - populate your local structs as data holder. and the next 1 or 2 links to follow you put in the queue.

maybe remember the already retrieved URL in a Array so you can check that you don't read some data twice.

NOTE: there is a call rate limit on the api site. if you create an public app then you should think how to serverside cache all the calls to be under 50/24h or 50/1min

muescha
  • 1,544
  • 2
  • 12
  • 22
  • Its not public, just for training. – odvan Feb 02 '17 at 02:05
  • So the logic is: start first session, put task into custom created queue, retrieve data, put it in struct, then i create another task to retrieve more deep level data. Where should i reload table and how populate cells? Just put some data from one struct and some data from other? But the problem is i need to do something like array of tasks. For example today its 8 games, 16 teams, i need 16 tasks? – odvan Feb 02 '17 at 02:13
0

I think i found the solution, should use 'dispatch group'.

odvan
  • 59
  • 2
  • 8