Alright, I have a piece of code that queries everything from an entity in CoreData, cycles through them all, processes the data, appends it to an array then deletes the CoreData object.
Saving the current context works just fine, but calling save on the parentContext
gives me a EXC_BAD_ACCESS
error.
Here's my code:
In my init method:
managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
managedObjectContext.parentContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
Essentially taking the main managedObjectContext
and setting it as my parentContext
. So far, no problem.
managedObjectContext.performBlock({ () -> Void in
do {
let fetchRequest = NSFetchRequest(entityName: "Changes")
fetchRequest.predicate = NSPredicate(format: "id BEGINSWITH[c] %@", idPrefix)
if let changes = try self.managedObjectContext.executeFetchRequest(fetchRequest) as? [Changes] {
for change in changes {
let json = change.json!
let newJson = change.newJson!
let changeId = change.id!
self.managedObjectContext.deleteObject(change)
let deserializedJson = try NSJSONSerialization.JSONObjectWithData(json.dataUsingEncoding(NSUTF8StringEncoding)!, options: [])
let deserializedNewJson = try NSJSONSerialization.JSONObjectWithData(newJson.dataUsingEncoding(NSUTF8StringEncoding)!, options: [])
updates.append([
"Id": changeId,
"NewJson": deserializedNewJson,
"Json": deserializedJson
])
}
try self.managedObjectContext.save()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
do {
try self.managedObjectContext.parentContext?.save()
}
catch {
completionHandler(error: error as NSError)
}
})
}
}
catch {
completionHandler(error: error as NSError)
}
})
I enabled NSZombies
to see of I could get more relevant information. When I try to execute the save of the parentContext
on the main thread, I get the following error:
*** -[CFString release]: message sent to deallocated instance 0x1579a0570
I have run into instances where this actually goes through without a problem, but most of the time, I get the error I just mentioned above.
Any ideas would be more than welcome!