ole / CoreDataDebugging

2 stars 1 forks source link

Core Data Multithreading Assertion despite using performBlock #1

Open ole opened 10 years ago

ole commented 10 years ago

When -com.apple.CoreData.ConcurrencyDebug 1 is activated, this code:

backgroundContext!.performBlock {
    let person = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: backgroundContext!) as NSManagedObject
    person.setValue("John Appleseed", forKey: "name")
    var potentialSaveError: NSError?
    let didSave = backgroundContext!.save(&potentialSaveError)
    if (didSave) {
        println("Saving successful")
    } else {
        let saveError = potentialSaveError!
        println("Saving failed with error: \(saveError)")
    }
}

fails with a multithreading violation assertion in the line let didSave = backgroundContext!.save(&potentialSaveError). I don't understand why since I have wrapped all access to the background context in a performBlock call.

@richardbuckle I would appreciate any input you have. Does it fail on your machine, too?

peymano commented 10 years ago

@ole Don't you want performBlockAndWait? Otherwise, this block is queued, then the enclosing function returns and ARC releases your persistent store coordinator, before this block gets a chance to execute.

ole commented 10 years ago

@peymano Thanks for the suggestion. I tried (and @richardbuckle did too) but it doesn't change the outcome. Anyway, it shouldn't matter. The persistent store coordinator is retained by the managed object context and the context lives at least until the end of the block's lifetime.