当前位置:  开发笔记 > IOS > 正文

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

如何解决《无法初始化应用程序的已保存数据》经验,为你挑选了2个好方法。

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

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

并在控制台中写道:

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; } ...返回错误错误Domain = NSCocoaErrorDomain Code = 134140"持久存储迁移失败,缺少映射模型." 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 = \"(),名称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,att ributeValueClassName 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 = \"(),名称pdfImage,isOptional 1,isTransient 0,实体通知,renamingIdentifier pdfImage,验证谓词(\n),warnings(\n),versionHashModifier(null)\n userInfo {\n},attributeType 1000,attributeValueClassName NSData,defaultValue(null)\"; \n title = \"(),name title,isOptional 1,isTransient 0,实体注意,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,entities {Notice ="()name Notice,managedObjectClassName toRep_Management.Notice,renamingIdentifier Notice,isAbstract 0,superentity name(null),properties {\n displayEndDate = \" (),名称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 = \"(),名称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 = \"(),名称pdf,isOptional 1,isTransient 0,实体通知,renamingIdentifier pdf,验证谓词(\n),警告(\n),versionHashModifier(null)\n userInfo {\n},attributeType 1000,attributeValueClassName NSData,defaultValue(null)\"; \n pdfImage = \"(),名称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,validation predicates(\n),warnings(\n),versionHashModifier(null)\n userInfo {\n},attributeType 700,attributeValueClassName NSString,defaultValue(null )\"; \n},子实体{\n},userInfo {\n},versionHashModifier(null),uniquenessConstraints(\n)"; },获取请求模板{},reason =无法找到或自动推断迁移的映射模型,NSUnderlyingError = 0x6080000511c0 {Error Domain = NSCocoaErrorDomain Code = 134190"(null)"UserInfo = {entity = Notice,property = pdfImage,reason =源和目标属性类型不兼容}}}与userInfo字典{NSUnderlyingError ="错误域= NSCocoaErrorDomain代码= 134190 \"(null)\"UserInfo = {entity = Notice,property = pdfImage,reason =源和目标属性类型不相容}"; 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 = \\"(),名称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 ,验证谓词(\\n),警告(\\n),versionHashModifier(null)\\n userInfo {\\n},attri buteType 800,attributeValueClassName NSNumber,defaultValue(null)\\"; \n pdf = \\"(),名称pdf,isOptional 1,isTransient 0,实体通知,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,validation predicates(\\n),warnings(\\n),versionHashModifier(null)\\n userInfo {\\n},attributeType 1800,attributeValueClassName(null),defaultValue(null)\\ "; \n title = \\"(),名称标题,isOptional 1,isTransient 0,实体通知,renamingIdentifier标题,验证谓词(\\n),警告(\\n),versionHashModifier(null)\\n userInfo {\\n},attributeType 700,attributeValueClassName NSString,defaultValue(null)\\"; \n},子实体{\n},userInfo {\n},versionHashModifier(null),uniquenessConstraints(\n)\"; \n},获取请求模板 {\ N}"; reason ="无法找到或自动推断迁移的映射模型"; 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 = \\"(),名称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 ,验证谓词(\\n),警告(\\n),versionHashModifier(null)\\n userInfo {\\n},attributeT ype 800,attributeValueClassName NSNumber,defaultValue(null)\\"; \n pdf = \\"(),名称pdf,isOptional 1,isTransient 0,实体通知,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,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},子实体{\n},userInfo {\n},versionHashModifier(null),uniquenessConstraints(\n)\"; \n} ,获取请求模板{\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!
    }
}()

有谁能够帮我?我认为我在创建适当的轻量级迁移方面失败了.这个假设是正确的,如何正确做到这一点?

非常感谢你!



1> Fırat Yenidü..:

你的问题是,

我最近更改了实体内部属性的类型.

解决方案很简单.从模拟器或真实设备中删除您的应用程序并重新安装.这个对我有用.



2> Tom Harringt..:

我最近更改了实体内部属性的类型。

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

有关自动轻量级迁移可以做什么的完整说明,请参阅关于主题的Apple文档。

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

创建非轻量级迁移的映射模型

删除您的应用程序并重新安装(如果它仍在开发中,而不是已经发布给用户)。

撤消您的更改并继续使用它。

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有