I'm trying to get JSON from a service in Swift 2
https://someapidomain.com/entries.json
[
1212,
1234,
2934,
....
]
https://someapidomain.com/entry/entry_id.json
{
"key1": "value1",
"key2": 23,
"key3": "value3,
...
}
Service must return:
[
{
"key1": "value1",
"key2": 23,
"key3": "value3,
...
},
{
"key1": "value1",
"key2": 23,
"key3": "value3,
...
},
....
]
struct ExampleService {
private static let baseURL = "https://someapidomain.com/"
private static let entries_per_page = 10
private static func getJSONResponse(url: String, callback: (AnyObject) -> () ){
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request, completionHandler: {
data, response, err -> Void in
do {
let jsonResponse = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
callback(jsonResponse)
}catch {
print("Error processing json")
}
})
task.resume()
}
static func getEntries(url: String, callback: ([[String: AnyObject]]) -> ()) {
var entries: [[String:AnyObject]] = []
let apiURL = baseURL + url
getJSONResponse(apiUrl) { (response) in
if let entryIds = response as? Array<Int> {
for entryId in storyIds[0..<entries_per_page] {
let entryURL = baseURL + "/entry/\(entryId).json"
getJSONResponse(entryURL) { (response) in
if let entry = response as? [String: AnyObject] {
print(entry)
entries.append(entry)
}
}
}
callback(entries)
}
}
}
}
}
Now when I call the service I get empty array
ExampleService.getEntries("/entries.json") { (response) in
print(response) // prints []
}
Whereas individual entries inside the loop prints the json. I think its because the callback is getting called before the for loop excecution is finished.
How can I fix this?
iostag to questions that are not directly related to iOS and could work for other Swift supporting platforms. Thank you.