我正在为服务器的后台长时间节省创建一个新的NSPersistentStoreCoordinator
。它工作正常但突然我看到生产中的崩溃,出现以下错误 -
CRASH_INFO_ENTRY_0 致命错误:后台上下文创建失败,错误错误Domain = NSCocoaErrorDomain Code = 134110"持久存储迁移期间发生错误。" UserInfo = {sourceURL = file:///var/mobile/Containers/Data/Application/8EF27C05-1755-49EE-B174-8B163CC7CC1D/Documents/App.sqlite,reason =无法迁移存储:尝试迁移期间的约束违规,destinationURL = file:///var/mobile/Containers/Data/Application/8EF27C05-1755-49EE-B174-8B163CC7CC1D/Documents/.App.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3,NSUnderlyingError = 0x170a56290 {Error Domain = NSCocoaErrorDomain Code = 134111&# 34;(NULL)" UserInfo = {reason =在尝试迁移期间违反约束,NSUnderlyingException =约束违规,_NSCoreDataOptimisticLockingFailureConflictsKey =("")}}}:file /Users/*******/Desktop/7.50/APPS/ LTG / project / modelBase / db / DatabaseManager.swift,第67行
Lookalike我有一个约束问题,因为这是我得到的错误 -
NSMigrationConstraintViolationError = 134111,// 由于违反唯一性约束,迁移失败
Bu,因为_NSCoreDataOptimisticLockingFailureConflictsKey is <null>
我无法追踪问题。
这是我的代码 -
lazy var backgroundSyncContext:NSManagedObjectContext = {
let psc = NSPersistentStoreCoordinator(managedObjectModel: NSManagedObjectModel.MR_defaultManagedObjectModel()!)
let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
context.undoManager = nil
context.persistentStoreCoordinator = psc
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
let paths = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
let applicationDocumentURL = paths.last!.absoluteString?.stringByReplacingOccurrencesOfString("%20", withString: " ")
let documentPath = paths.last!
let storeUrl = documentPath.URLByAppendingPathComponent("Prep4\(AppConfig.sharedInstance.examName).sqlite")
do {
try context.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeUrl, options: mOptions)
print("Created background context")
} catch (let error){
fatalError("Background context creation failed with error \(error)")
}
return context
}()
修改
好的,我有一个猜测 - 是我为模型添加约束的最新版本。是否有可能用户设备上的旧版本包含对新约束无效的重复项,因此迁移失败。
这听起来合理吗?
由于 沙尼