swiftmacoscocoacore-datacore-data-migration

Failed to initialize the application's saved data


I recently changed the type of an attribute inside an entity. I created before a new model version of the database. But something doesn't work properly. If I run the project, the App throws an error:

Failed to initialize the application's saved data

And in the console there is written:

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}"; }

This is very confusing to me. I also tried to set up a lightweight migration inside the AppDelegate.swift like this:

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!
    }
}()

Can anybody help me? I think I failed in creating a proper lightweight migration. Is this assumption right and how to do this right?

Thank you very much!


Solution

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

    There's your problem. Automatic migration doesn't support changing attribute types. There's no general-purpose way for Core Data to figure out how you want to convert some attribute type to some other attribute type, so you can't ask it to migrate automatically.

    For a full explanation of what automatic lightweight migration can do, see Apple's documentation on the topic.

    At this point you can do one of the following: