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

时间:2016-01-04 22:28:38

标签: swift macos cocoa core-data core-data-migration

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

  

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

在控制台中写道:

  

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 {       注意="()名称注意,managedObjectClassName toRep_Management.Notice,renamingIdentifier注意,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)\&#34 ;; \ 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)\&#34 ;; \ n isOnline = \"(),name isOnline,isOptional 1,isTransient 0,entity Notice,renamingIdentifier isOnline,validation predicates(\ n),警告(\ n),versionHashModifier(null)\ n userInfo {\ n},attributeType 800,attributeValueClassN ame NSNumber,defaultValue(null)\&#34 ;; \ 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)\&#34 ;; \ n pdfImage = \"(),name pdfImage,isOptional 1,isTransient 0,实体通知,renamingIdentifier pdfImage,验证谓词(\ n),警告(\ n),versionHashModifier(null)\ n userInfo {\ n},attributeType 1000,attributeValueClassName NSData,defaultValue(null)\&#34 ;; \ 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)\&#34 ;; \ n},子实体{\ n},userInfo {\ n},versionHashModifier(null),uniquenessConstraints(\ n)&#34 ;;   ,获取请求模板{   },destinationModel =()isEditable 0,entities {       注意="()名称注意,managedObjectClassName toRep_Management.Notice,renamingIdentifier注意,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)\&#34 ;; \ 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)\&#34 ;; \ n isOnline = \"(),name isOnline,isOptional 1,isTransient 0,entity Notice,renamingIdentifier isOnline,validation predicates(\ n),警告(\ n),versionHashModifier(null)\ n userInfo {\ n},attributeType 800,attributeValueClassN ame NSNumber,defaultValue(null)\&#34 ;; \ 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)\&#34 ;; \ 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)\&#34 ;; \ n},子实体{\ n},userInfo {\ n},versionHashModifier(null),uniquenessConstraints(\ n)&#34 ;;   ,获取请求模板{   },reason =无法找到或自动推断迁移的映射模型,NSUnderlyingError = 0x6080000511c0 {Error Domain = NSCocoaErrorDomain Code = 134190"(null)" userInfo = {entity = Notice,property = pdfImage,reason =源和目标属性类型不兼容}}} with userInfo dictionary {       NSUnderlyingError ="错误域= NSCocoaErrorDomain代码= 134190 \"(null)\" UserInfo = {entity = Notice,property = pdfImage,reason = Source和destination属性类型不兼容}&#34 ;;       destinationModel ="()isEditable 0,entities {\ n 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)\\&#34 ;; \ n displayStartDate = \\"(),name displayStartDate,isOptional 1,isTransient 0,entity Notice,renamingIdentifier displayStartDate,validation predicates (\\ n),警告(\\ n),versionHashModifier(null)\\ n userInfo {\\ n},attributeType 900,attributeValueClassName NSDate,defaultValue(null)\\&#34 ;; \ n isOnline = \\ "(),name isOnline,isOptional 1,isTransient 0,entity Notice,renamingIdentifier isOnline,validation predicates(\\ n),warnings(\\ n),versionHash修饰符(null)\\ n userInfo {\\ n},attributeType 800,attributeValueClassName NSNumber,defaultValue(null)\\&#34 ;; \ 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),警告(\\ n),versionHashModifier( null)\\ n userInfo {\\ n},attributeType 1800,attributeValueClassName(null),defaultValue(null)\\&#34 ;; \ 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},版本H. ashModifier(null),uniquenessConstraints(\ n)\&#34 ;; \ n},获取请求模板{\ n}&#34 ;;       reason ="无法找到或自动推断迁移的映射模型&#34 ;;       sourceModel ="()isEditable 1,entities {\ n 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)\\&#34 ;; \ n displayStartDate = \\"(),name displayStartDate,isOptional 1,isTransient 0,entity Notice,renamingIdentifier displayStartDate,validation predicates (\\ n),警告(\\ n),versionHashModifier(null)\\ n userInfo {\\ n},attributeType 900,attributeValueClassName NSDate,defaultValue(null)\\&#34 ;; \ n isOnline = \\ "(),name isOnline,isOptional 1,isTransient 0,entity Notice,renamingIdentifier isOnline,validation predicates(\\ n),warnings(\\ n),versionHashModif ier(null)\\ n userInfo {\\ n},attributeType 800,attributeValueClassName NSNumber,defaultValue(null)\\&#34 ;; \ 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),警告(\\ n),versionHashModifier( null)\\ n userInfo {\\ n},attributeType 1000,attributeValueClassName NSData,defaultValue(null)\\&#34 ;; \ 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)\\&# 34 ;; \ n},子实体{\ n},userInfo {\ n},versionHashMo difier(null),uniquenessConstraints(\ n)\&#34 ;; \ n},获取请求模板{\ n}&#34 ;;   }

这对我来说非常困惑。我还尝试在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!
    }
}()

任何人都可以帮助我吗?我认为我在创建适当的轻量级迁移方面失败了。这个假设是正确的,如何做到这一点?

非常感谢!

3 个答案:

答案 0 :(得分:6)

你的问题是,

  

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

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

答案 1 :(得分:5)

  

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

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

有关自动轻量级迁移可以执行的操作的完整说明,请参阅Apple's documentation on the topic

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

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

答案 2 :(得分:0)

如果您的代码正确无误,请尝试删除应用,然后重新构建。对我来说这是工作

我的回答是同一个汤姆