gpt4 book ai didi

swift - 无法初始化应用程序保存的数据

转载 作者:行者123 更新时间:2023-12-03 17:27:49 27 4
gpt4 key购买 nike

我最近更改了实体内属性的类型。我之前创建了一个新模型版本的数据库。但有些东西无法正常工作。如果我运行该项目,应用程序会抛出错误:

Failed to initialize the application's saved data

控制台中写着:

2016-01-04 23:21:16.805 toRep_Management[36979:10809172] CoreData: error: -addPersistentStoreWithType:XML configuration:(null) URL:file:///Users/Tom/Library/Application%20Support/de.toEducate.toRep_Management/CocoaAppCD.storedata options:{ NSInferMappingModelAutomaticallyOption = 1; NSMigratePersistentStoresAutomaticallyOption = 1; } ... returned error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model." UserInfo={sourceModel=() isEditable 1, entities { 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, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n displayStartDate = \"(), name displayStartDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n isOnline = \"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)\";\n pdf = \"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\";\n pdfImage = \"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\";\n title = \"(), name title, isOptional 1, isTransient 0, entity Notice, renamingIdentifier title, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)"; }, fetch request templates { }, destinationModel=() isEditable 0, entities { 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, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n displayStartDate = \"(), name displayStartDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n isOnline = \"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)\";\n pdf = \"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\";\n pdfImage = \"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 1800 , attributeValueClassName (null), defaultValue (null)\";\n title = \"(), name title, isOptional 1, isTransient 0, entity Notice, renamingIdentifier title, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)"; }, fetch request templates { }, 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 are incompatible}}} with userInfo dictionary { NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=134190 \"(null)\" UserInfo={entity=Notice, property=pdfImage, reason=Source and destination attribute types are incompatible}"; destinationModel = "() isEditable 0, entities {\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, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n displayStartDate = \\"(), name displayStartDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n isOnline = \\"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)\\";\n pdf = \\"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\\";\n pdfImage = \\"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1800 , attributeValueClassName (null), defaultValue (null)\\";\n title = \\"(), name title, isOptional 1, isTransient 0, entity Notice, renamingIdentifier title, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)\";\n}, fetch request templates {\n}"; reason = "Can't find or automatically infer mapping model for migration"; sourceModel = "() isEditable 1, entities {\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, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n displayStartDate = \\"(), name displayStartDate, isOptional 1, isTransient 0, entity Notice, renamingIdentifier displayStartDate, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\\";\n isOnline = \\"(), name isOnline, isOptional 1, isTransient 0, entity Notice, renamingIdentifier isOnline, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)\\";\n pdf = \\"(), name pdf, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdf, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\\";\n pdfImage = \\"(), name pdfImage, isOptional 1, isTransient 0, entity Notice, renamingIdentifier pdfImage, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 1000 , attributeValueClassName NSData, defaultValue (null)\\";\n title = \\"(), name title, isOptional 1, isTransient 0, entity Notice, renamingIdentifier title, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)\";\n}, fetch request templates {\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!
}
}()

有人可以帮我吗?我认为我未能创建适当的轻量级迁移。这个假设正确吗?如何做到这一点?

非常感谢!

最佳答案

I recently changed the type of an attribute inside an entity.

这就是你的问题。自动迁移不支持更改属性类型。 Core Data 没有通用的方法来确定您希望如何将某些属性类型转换为其他属性类型,因此您不能要求它自动迁移。

有关自动轻量级迁移功能的完整说明,请参阅 Apple's documentation on the topic .

此时您可以执行以下操作之一:

  • 创建非轻量级迁移的映射模型
  • 删除您的应用并重新安装(如果它仍在开发中,而不是已经向用户发布)。
  • 撤消您的更改并接受它。

关于swift - 无法初始化应用程序保存的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34601027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com