[英]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. 我正在尝试将数据添加到我的数据模型中,因此要进行测试,我正在打印通过Alamofire获取的信息,但是我的问题是,由于某些数据需要再次调用api,因此在我打印它时它会变为null。 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模型
You should make your api call in background and after it's finished populate your data on main queue. 您应该在后台进行api调用,并在完成后将数据填充到主队列中。 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
. 在上面的代码中,首先在
background
向服务器发出请求,如果在main
队列中得到响应,则将填充variable name
。 Also it's better to change your api call function to something like that: 另外,最好将您的api调用函数更改为类似的代码:
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. 这样,您就可以处理错误以及成功获取数据的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.