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, "https://sysco-dev.madmobile.com/api/products.pricing", 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.
I solved my problem by explicitly calling the for loop as async using dispatch_async
. I then invoke the main thread again for the closure and to change a bool flag of mine.
func parseInOrderGuideProducts(completion: Bool -> ()) {
var parsedJSON = JSON(rawOrderGuideJSON!)
var resultset = parsedJSON["resultset"]
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
for (key, subJson) in resultset {
println(key)
var newProduct: Product = Product()
newProduct.title = key as String
newProduct.id = resultset[key]["materialId"].string
var price = resultset[key]["price"].double
newProduct.price = "$\(price!)"
newProduct.weight = resultset[key]["totalWeight"].string
newProduct.image = self.getOrderGuidePhotoForID(newProduct.id!)
self.orderGuideItemsList.append(newProduct)
}
dispatch_async(dispatch_get_main_queue()) {
completion(true)
self.finishedPopulatingOrderGuide = true
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.