Ron Pelayo
Ron Pelayo

Reputation: 655

Wait for multiple Alamofire request

I'm trying to add data to my data model so to test it I'm printing the info fetched via Alamofire but my problem is since some data needs to call the api again it becomes null when I print it. Here's my code

Code for getting the person's data

func printAPI(){

swApiHandler.requestSWPApi("http://swapi.co/api/people", completionHandler: {(response, error) in

    let json = JSON(response!)
    let jsonResult = json["results"]

    for (index,person):(String, JSON) in jsonResult{
        let name = person["name"].stringValue
        let height = person["height"].intValue
        let mass = person["mass"].intValue
        let hairColor = person["hair_color"].stringValue
        let skinColor = person["skin_color"].stringValue
        let eyeColor = person["eye_color"].stringValue
        let birthYear = person["birth_year"].stringValue
        let gender = person["gender"].stringValue
        let homeWorldUrl = person["homeworld"].stringValue
        let homeWorldNameKey = "name"
        let homeWorld = self.getSWApiSpecificValue(homeWorldUrl, strKey: homeWorldNameKey)

        print("Name: \(name)")
        print("Height: \(height)")
        print("Mass: \(mass)")
        print("Hair Color: \(hairColor)")
        print("Skin Color: \(skinColor)")
        print("Eye Color: \(eyeColor)")
        print("Birth Year: \(birthYear)")
        print("Gender: \(gender)")
        print("Home World: \(homeWorld)")
        print("------------------------------")
    }
})

}

Code for getting the specific value

  func getSWApiSpecificValue(strUrl: String, strKey: String) -> String{
    var name = ""
    swApiHandler.requestSWPApi(strUrl, completionHandler: {(response,error) in
        let json = JSON(response!)
        print(json[strKey].stringValue)
        name = json[strKey].stringValue
    })

    return name
}

If you want to know the JSON Model here it is

JSON Model

And for running the code here's the output Output

Upvotes: 1

Views: 848

Answers (1)

amanbolat
amanbolat

Reputation: 81

You should make your api call in background and after it's finished populate your data on main queue. Just change your code to get specific value to this one:

func getSWApiSpecificValue(strUrl: String, strKey: String) -> String{
  var name = ""
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { () -> Void in
swApiHandler.requestSWPApi(strUrl, completionHandler: {(response,error) in
    dispatch_async(dispatch_get_main_queue()) {
      let json = JSON(response!)
      print(json[strKey].stringValue)
      name = json[strKey].stringValue
      return name
    }
  })
 }
}

In code above first you get make a request to the server in background and if you get response in main queue will populate you variable name. Also it's better to change your api call function to something like that:

func getDataFromServer(ulr: String, success: (([AnyObject]) -> Void)?, failure: (error: ErrorType) -> Void){
}

In this way you can handle your errors and if success get your data.

Upvotes: 1

Related Questions