【发布时间】:2016-04-08 15:34:06
【问题描述】:
我最近更改了实体中属性的类型。我之前创建了一个新模型版本的数据库。但是有些东西不能正常工作。 如果我运行项目,应用程序会抛出错误:
初始化应用保存数据失败
在控制台中写着:
2016-01-04 23:21:16.805 toRep_Management[36979:10809172] CoreData: 错误:-addPersistentStoreWithType:XML 配置:(null) URL:file:///Users/Tom/Library/Application%20Support/de .toEducate.toRep_Management/CocoaAppCD.storedata 选项:{ NSInferMappingModelAutomaticallyOption = 1; NSMigratePersistentStoresAutomaticallyOption = 1; } ... 返回错误 错误域 = NSCocoaErrorDomain 代码 = 134140 “持久存储迁移失败,缺少映射模型。” UserInfo={sourceModel=() isEditable 1,实体{ Notice = "() name Notice, managedObjectClassName toRep_Management.Notice, renamingIdentifier Notice, isAbstract 0, superentity name (null), properties {\n displayEndDate = \"(), name displayEndDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayEndDate , 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n displayStartDate = \"(), name displayStartDate , isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\" ;\n isOnline = \"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n},属性类型 800 ,属性值类名称 NSNumber,默认值ue (null)\";\n pdf = \"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\";\n pdfImage = \"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, 验证谓词 (\n),警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\";\n title = \"(), name title, isOptional 1, isTransient 0, entity注意,renamingIdentifier 标题,验证谓词 (\n),警告 (\n),versionHashModifier (null)\n userInfo {\n},attributeType 700,attributeValueClassName NSString,defaultValue (null)\";\n},子实体 {\ n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)"; }, 获取请求模板 { },destinationModel=() isEditable 0,实体{ Notice = "() name Notice, managedObjectClassName toRep_Management.Notice, renamingIdentifier Notice, isAbstract 0, superentity name (null), properties {\n displayEndDate = \"(), name displayEndDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayEndDate , 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n displayStartDate = \"(), name displayStartDate , isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\" ;\n isOnline = \"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n},属性类型 800 ,属性值类名称 NSNumber,默认值ue (null)\";\n pdf = \"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\";\n pdfImage = \"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, 验证谓词 (\n),警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1800 , attributeValueClassName (null), defaultValue (null)\";\n title = \"(), name title, isOptional 1, isTransient 0 , entity Notice, renamingIdentifier title, 验证谓词 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n}, 子实体{\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)"; }, 获取请求模板 { }, reason=Can't find or automatically infer mapping model for migration, NSUnderlyingError=0x6080000511c0 {Error Domain=NSCocoaErrorDomain Code=134190 "(null)" UserInfo={entity=Notice, property=pdfImage, reason=Source and destination attribute types与 userInfo 字典不兼容}}} { NSUnderlyingError = "错误域=NSCocoaErrorDomain Code=134190 \"(null)\" UserInfo={entity=Notice, property=pdfImage, reason=Source 和destination 属性类型不兼容}"; destinationModel = "() isEditable 0, entity {\n Notice = \"() name Notice, managedObjectClassName toRep_Management.Notice, renamingIdentifier Notice, isAbstract 0, superentity name (null), properties {\n displayEndDate = \\"(), name displayEndDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayEndDate, 验证谓词 (\\n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n displayStartDate = \\"(), name displayStartDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, 验证谓词(\\n), 警告(\\n), versionHashModifier ( null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n isOnline = \\"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline , 验证谓词 (\\n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)\\";\n pdf = \\"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, 验证谓词 (\\ n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\\";\n pdfImage = \\"(), name pdfImage , isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, 验证谓词 (\\n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1800, attributeValueClassName (null), defaultValue (null)\\";\n title = \\"(), name title, isOptional 1, isTransient 0, entity Notice, renamingIdentifier title, 验证谓词(\\n), warnings (\\n), versionHashModifier ( null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\\";\n}, 子实体 {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\ n)\";\n}, fe tch 请求模板 {\n}"; reason = "找不到或无法自动推断迁移的映射模型"; sourceModel = "() isEditable 1, entity {\n Notice = \"() name Notice, managedObjectClassName toRep_Management.Notice, renamingIdentifier Notice, isAbstract 0, superentity name (null), properties {\n displayEndDate = \\"(), name displayEndDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayEndDate, 验证谓词 (\\n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n displayStartDate = \\"(), name displayStartDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, 验证谓词(\\n), 警告(\\n), versionHashModifier ( null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n isOnline = \\"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline , 验证谓词 (\\n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n} , attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)\\";\n pdf = \\"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, 验证谓词 (\\n), 警告 ( \\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1000, attributeValueClassName NSData, defaultValue (null)\\";\n pdfImage = \\"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, 验证谓词 (\\n), 警告 (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\\" ;\n title = \\"(), name title, isOptional 1, isTransient 0, entity Notice, renamingIdentifier title, 验证谓词(\\n), 警告(\\n), versionHashModifier (null)\\n userInfo { \\n}, attributeType 700, attributeValueClassName NSString, defaultValue (null)\\";\n}, 子实体 {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)\";\n }, 获取 r任务模板 {\n}"; }
这让我很困惑。我还尝试在 AppDelegate.swift 中设置一个轻量级迁移,如下所示:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. (The directory for the store is created, if necessary.) This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
let fileManager = NSFileManager.defaultManager()
var failError: NSError? = nil
var shouldFail = false
var failureReason = "There was an error creating or loading the application's saved data."
// Make sure the application files directory is there
do {
let properties = try self.applicationDocumentsDirectory.resourceValuesForKeys([NSURLIsDirectoryKey])
if !properties[NSURLIsDirectoryKey]!.boolValue {
failureReason = "Expected a folder to store application data, found a file \(self.applicationDocumentsDirectory.path)."
shouldFail = true
}
} catch {
let nserror = error as NSError
if nserror.code == NSFileReadNoSuchFileError {
do {
try fileManager.createDirectoryAtPath(self.applicationDocumentsDirectory.path!, withIntermediateDirectories: true, attributes: nil)
} catch {
failError = nserror
}
} else {
failError = nserror
}
}
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = nil
let migrateOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
if failError == nil {
coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CocoaAppCD.storedata")
do {
try coordinator!.addPersistentStoreWithType(NSXMLStoreType, configuration: nil, URL: url, options: migrateOptions)
} catch {
failError = error as NSError
}
}
if shouldFail || (failError != nil) {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
if failError != nil {
dict[NSUnderlyingErrorKey] = failError
}
let error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
NSApplication.sharedApplication().presentError(error)
abort()
} else {
return coordinator!
}
}()
有人可以帮我吗?我认为我未能创建适当的轻量级迁移。这个假设是否正确以及如何做到这一点?
非常感谢!
【问题讨论】:
标签: swift macos cocoa core-data core-data-migration