当我尝试保存上下文时(第二次调用privateManagedObjectContext.hasChanges方法),我收到EXC_BAD_INSTRUCTION错误。
以下是保存上下文的方法:
func saveContext(#wait: Bool, completion: ((error: NSError?) -> ())?) {
if mainManagedObjectContext.hasChanges || privateManagedObjectContext.hasChanges {
if mainManagedObjectContext.hasChanges {
mainManagedObjectContext.performBlockAndWait({ () -> Void in
var error: NSError? = nil
if !self.mainManagedObjectContext.save(&error) {
println("Error saving main managed object context \(error)")
if completion != nil {
completion!(error: error)
}
return
}
})
}
let saveBlock: () -> Void = {
var error: NSError? = nil
if self.privateManagedObjectContext.save(&error) {
if completion != nil {
completion!(error: nil)
}
} else {
println("Error saving private managed object context \(error)")
if completion != nil {
completion!(error: error)
}
}
}
if self.privateManagedObjectContext.hasChanges {
if (wait) {
self.privateManagedObjectContext.performBlockAndWait(saveBlock)
} else {
self.privateManagedObjectContext.performBlock(saveBlock)
}
return
}
if completion != nil {
completion!(error: nil)
}
} else {
// No changes
if completion != nil {
completion!(error: nil)
}
}
}
我声明了这样的背景:
privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateManagedObjectContext.persistentStoreCoordinator = storeCoordinator
mainManagedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
mainManagedObjectContext.parentContext = privateManagedObjectContext
我在这里错了什么?