我试图从Swift 2中的服务获取JSON
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)
}
}
}
}
}
现在当我调用服务时,我得到空数组
ExampleService.getEntries("/entries.json") { (response) in
print(response) // prints []
}
而循环内的各个条目打印json。我认为这是因为在for循环执行完成之前调用了回调。
我该如何解决这个问题?
答案 0 :(得分:2)
修改后的函数getEntries
概述了如何使用dispatch_group完成此任务。
此外,您必须确保您的函数getJSONResponse
始终调用完成处理程序,即使它失败也是如此。这是因为,您必须确保函数调用dispatch_enter
和dispatch_leave
是平衡的。
我建议在这个意义上修改getEntries
函数,即使失败也总是调用完成处理程序。相应地修改完成处理程序的签名。
static func getEntries(url: String, callback: ([[String: AnyObject]]) -> ()) {
var entries: [[String:AnyObject]] = []
getJSONResponse(url) { (response) in
let queue = dispatch_queue_create("myqueue", nil)
let group = dispatch_group_create()
if let entryIds = response as? Array<Int> {
for entryId in entryIds {
dispatch_group_enter(group)
let entryURL = baseURL + "/entry/\(entryId).json"
getJSONResponse(entryURL) { (response) in
if let entry = response as? [String: AnyObject] {
print(entry)
entries.append(entry)
}
dispatch_group_leave(group)
}
}
}
dispatch_group_notify(group, queue) {
callback(entries)
}
}
}
答案 1 :(得分:1)
以下示例创建,进入和离开
let jsonGroup = dispatch_group_create()
for x in json{
dispatch_group_enter(getDeparturesGroup)
// Some code...
}
dispatch_group_wait(jsonGroup, DISPATCH_TIME_FOREVER)