CoreData:使用Swift在私有队列上的EXC_BAD_INSTRUCTION

时间:2015-01-08 22:33:34

标签: ios core-data swift

当我尝试保存上下文时(第二次调用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

我在这里错了什么?

0 个答案:

没有答案