Xcode8 和 Core Data 自动轻量级迁移
Xcode8 and Core Data automatic lightweight migration
我一直在尝试使用 Xcode8 对我的核心数据模式执行轻量级迁移,然后 运行 遇到了一些奇怪的问题。更新后第一次加载商店时,选项字典中的 NSMigratePersistentStoresAutomaticallyOption
和 NSInferMappingModelAutomaticallyOption
标志都设置为 YES,我 运行 进入此错误:
2016-10-01 09:59:17.307862 CJournal[1162:549816] loadLocalStoreIntoPSC using store = file:///private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite
2016-10-01 09:59:18.373568 CJournal[1162:549816] [error] error: Failed to delete support directory for store: /private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/.ContactsJournal.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3
2016-10-01 09:59:18.656164 CJournal[1162:549816] [error] error: CoreData: error: (migration) migration failed with error (null)
2016-10-01 09:59:18.664001 CJournal[1162:549816] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSSQLiteErrorDomain Code=8 "(null)" UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite, Source database Path=/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/.ContactsJournal.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, reason=Failed to replace destination database} with userInfo dictionary {
NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite";
"Source database Path" = "/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/.ContactsJournal.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3";
reason = "Failed to replace destination database";
}
这在 iOS10 模拟器和 iOS10 设备上都会发生。奇怪的是,第二个 运行 我 运行 应用程序尝试迁移,运行 没问题,没有任何错误!
这是我用来添加商店的代码:
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:@YES, NSMigratePersistentStoresAutomaticallyOption, @YES, NSInferMappingModelAutomaticallyOption, nil];
NSPersistentStore *newStore = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:localStoreURL options:options error:&error];
我也试过在 "Arguments Passed on Launch" 中使用 -com.apple.CoreData.MigrationDebug 1
标志,但它不会产生任何可能有用的输出。
知道可能出了什么问题,或者如果迁移由于某些验证问题而失败时如何调试?
我发现了问题...在轻量级迁移之前,我正在对现有数据库进行备份,因此我创建了一个 NSPersistentStoreCoordinator
(与通常的堆栈分开)并添加了 persistentStore没有任何轻量级迁移选项。这显然锁定了存储文件,该文件阻止了主核心数据堆栈 persistentStoreCoordinator
上的迁移,即使在备份过程完成后也是如此。修复是从备份中删除 persistentStore persistentStoreCoordinator
。这确保迁移工作正常并且 addPersistentStoreWithType
此时不会抛出错误。
我一直在尝试使用 Xcode8 对我的核心数据模式执行轻量级迁移,然后 运行 遇到了一些奇怪的问题。更新后第一次加载商店时,选项字典中的 NSMigratePersistentStoresAutomaticallyOption
和 NSInferMappingModelAutomaticallyOption
标志都设置为 YES,我 运行 进入此错误:
2016-10-01 09:59:17.307862 CJournal[1162:549816] loadLocalStoreIntoPSC using store = file:///private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite
2016-10-01 09:59:18.373568 CJournal[1162:549816] [error] error: Failed to delete support directory for store: /private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/.ContactsJournal.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3
2016-10-01 09:59:18.656164 CJournal[1162:549816] [error] error: CoreData: error: (migration) migration failed with error (null)
2016-10-01 09:59:18.664001 CJournal[1162:549816] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSSQLiteErrorDomain Code=8 "(null)" UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite, Source database Path=/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/.ContactsJournal.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, reason=Failed to replace destination database} with userInfo dictionary {
NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/ContactsJournal.sqlite";
"Source database Path" = "/private/var/mobile/Containers/Shared/AppGroup/04521C25-2FE6-4CFD-ACB5-70550B63499E/.ContactsJournal.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3";
reason = "Failed to replace destination database";
}
这在 iOS10 模拟器和 iOS10 设备上都会发生。奇怪的是,第二个 运行 我 运行 应用程序尝试迁移,运行 没问题,没有任何错误!
这是我用来添加商店的代码:
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:@YES, NSMigratePersistentStoresAutomaticallyOption, @YES, NSInferMappingModelAutomaticallyOption, nil];
NSPersistentStore *newStore = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:localStoreURL options:options error:&error];
我也试过在 "Arguments Passed on Launch" 中使用 -com.apple.CoreData.MigrationDebug 1
标志,但它不会产生任何可能有用的输出。
知道可能出了什么问题,或者如果迁移由于某些验证问题而失败时如何调试?
我发现了问题...在轻量级迁移之前,我正在对现有数据库进行备份,因此我创建了一个 NSPersistentStoreCoordinator
(与通常的堆栈分开)并添加了 persistentStore没有任何轻量级迁移选项。这显然锁定了存储文件,该文件阻止了主核心数据堆栈 persistentStoreCoordinator
上的迁移,即使在备份过程完成后也是如此。修复是从备份中删除 persistentStore persistentStoreCoordinator
。这确保迁移工作正常并且 addPersistentStoreWithType
此时不会抛出错误。