I have a big JSON list of items that I download, which I believe happens asynchronously (good) but when I loop through the JSON to turn them into objects ("Product" objects to be exact), the whole app freezes as the for loop executes.
The offending call
self.dm.getOrderGuideData({ (time: CFAbsoluteTime) in
self.dm.parseInOrderGuideProducts({ (completion: Bool) in
})
})
I put them in closures to prevent this but it doesn't seem to be working.
func getOrderGuideData(completion: (time: CFAbsoluteTime) -> ()) {
let startTime = CFAbsoluteTimeGetCurrent()
Alamofire.request(.POST, "http://ift.tt/1SHZNzm", parameters: orderGuideRequest, encoding: .JSON) .responseJSON { (req, res, json, error) in
if error != nil {
println("\n\nOG ERROR: \(error!)\n\n")
println(req)
println(res)
let endTime = CFAbsoluteTimeGetCurrent() - startTime
completion(time: endTime)
}
else {
var jsonForError = JSON(json!)
if jsonForError["errors"] != nil {
println("Order Guide Success")
self.rawOrderGuideJSON = json
let endTime = CFAbsoluteTimeGetCurrent() - startTime
completion(time: endTime)
}
else {
var error = jsonForError["errors"]
println(error)
let endTime = CFAbsoluteTimeGetCurrent() - startTime
completion(time: endTime)
}
}
}
}
func parseInOrderGuideProducts(completion: Bool -> ()) {
var parsedJSON = JSON(rawOrderGuideJSON!)
var resultset = parsedJSON["resultset"]
for (key, subJson) in resultset {
println(key)
var newProduct: Product = Product()
newProduct.title = key as String
// newProduct.id = parsedJSON["resultset"][key]["materialId"].string
// newProduct.image = getOrderGuidePhotoForID(newProduct.id!)
newProduct.id = resultset[key]["materialId"].string
var price = resultset[key]["price"].double
newProduct.price = "$\(price!)"
newProduct.weight = resultset[key]["totalWeight"].string
orderGuideItemsList.append(newProduct)
}
completion(true)
}
Any ideas on how I can fix this? The output to the console scrolls fine as a the keys are printed (see parseInOrderGuideProducts) but execution on the phone or simulator halts.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire