Hi guys, does any of you have any experience with Concurrent programming in Swift/Objective-C? Im working on a JSON parser that needs to parse large arrays and performance on iPhone 4 with sequential parsing isnt very good. So im trying to get the parser to work by parsing the arrays in multiple threads, but my locks dont seem to work properly. Basically what I need is this:
Code:
func parseArray(jsonArray: [JSON]) -> [AnyObject] {
var array = [AnyObject]()
let size = count(jsonArray)
var lockA = lock(0)// create Lock
var lockB = lock(size)
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
for var i = 0; i < size; i++
{
let index = i
let jsonObject: JSON = jsonArray[index]
dispatch_async(dispatch_get_global_queue(priority, 0)) {
if let parsedObject : AnyObject = self.parseObject(jsonObject)
{
wait(lockA)
if index < count(array) {
array.insert(parsedObject, atIndex: index)
} else {
array.append(parsedObject)
}
release(lockA)
release(lockB)
} else {
release(lockB)
}
}
}
wait(lockB) // Wait untill all released
return array
}
I tried using a dispatch_semaphore_create(1) for lockA and a dispatch_group_create() for lockB, but whatever I do lockB never seems to unlock, deadlocking the whole process. Note that this funcion may also be called by parseObject (on a different instance and usually even different extension of the class)
Any ideas?