【问题标题】:Core Data Error: "Can't find model for source store"核心数据错误:“找不到源存储模型”
【发布时间】:2016-05-12 09:44:09
【问题描述】:

我刚刚在 CoreData 中删除了我的实体的一个属性,之后出现了这个错误:

2016-02-02 21:37:54.499 toRep_Management[32110:4679391] CoreData: 错误: -addPersistentStoreWithType:XML 配置:(null) URL:file:///Users/Tom/Library/Application%20Support/de .toEducate.toRep_Management/CocoaAppCD.storedata 选项:{ NSInferMappingModelAutomaticallyOption = 1; NSMigratePersistentStoresAutomaticallyOption = 1; } ... 返回错误错误域 = NSCocoaErrorDomain 代码 = 134130 “持久存储迁移失败,缺少源托管对象模型。” UserInfo={URL=file:///Users/Tom/Library/Application%20Support/de.toEducate.toRep_Management/CocoaAppCD.storedata, metadata={type = immutable dict, count = 6, 条目 => 0:{内容=“NSStoreUUID”}={内容=“A3FD787E-495C-493D-A3B9-2E7F7925EF7C”} 3:{内容=“NSStoreModelVersionIdentifiers”}=( “” ) 4 : {contents = "NSStoreType"} = {contents = "XML"} 10 : {contents = "NSPersistenceFrameworkVersion"} = {value = +641, type = kCFNumberSInt64Type} 11 : {contents = "NSStoreModelVersionHashes"} = {type = mutable dict, count = 1, 条目 => 0:注意 = {长度 = 32,容量 = 32,字节 = 0xd22eaaf9bb53e406e8914544584e0e72 ... 0899bacb36b735bd} } 12:{contents = "NSStoreModelVersionHashesVersion"} = {值 = +3,类型 = kCFNumberSInt64Type} } , reason=Can't find model for source store} 使用 userInfo 字典 { URL = "file:///Users/Tom/Library/Application%20Support/de.toEducate.toRep_Management/CocoaAppCD.storedata"; 元数据 = { NSPersistenceFrameworkVersion = 641; NSStoreModelVersionHashes = { 注意 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( “” ); NSStoreType = XML; NSStoreUUID = "A3FD787E-495C-493D-A3B9-2E7F7925EF7C"; }; reason = "找不到源存储的模型"; } 2016-02-02 21:37:54.499 toRep_Management[32110:4679391] CoreData:注释:NSPersistentStoreCoordinator 的当前模型哈希是 { 注意 = ; }

我以为我正确实施了轻量级迁移,但现在我不确定了。

我的 AppDelegate:

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application
}

func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}

// MARK: - Core Data stack

lazy var applicationDocumentsDirectory: NSURL = {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "de.toEducate.toRep_Management" in the user's Application Support directory.
    let urls = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)
    let appSupportURL = urls[urls.count - 1]
    return appSupportURL.URLByAppendingPathComponent("de.toEducate.toRep_Management")
}()

lazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = NSBundle.mainBundle().URLForResource("toRep_Management", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

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

lazy var managedObjectContext: NSManagedObjectContext = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

// MARK: - Core Data Saving and Undo support

@IBAction func saveAction(sender: AnyObject!) {
    // Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
    if !managedObjectContext.commitEditing() {
        NSLog("\(NSStringFromClass(self.dynamicType)) unable to commit editing before saving")
    }
    if managedObjectContext.hasChanges {
        do {
            try managedObjectContext.save()
        } catch {
            let nserror = error as NSError
            NSApplication.sharedApplication().presentError(nserror)
        }
    }
}

func windowWillReturnUndoManager(window: NSWindow) -> NSUndoManager? {
    // Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
    return managedObjectContext.undoManager
}

func applicationShouldTerminate(sender: NSApplication) -> NSApplicationTerminateReply {
    // Save changes in the application's managed object context before the application terminates.

    if !managedObjectContext.commitEditing() {
        NSLog("\(NSStringFromClass(self.dynamicType)) unable to commit editing to terminate")
        return .TerminateCancel
    }

    if !managedObjectContext.hasChanges {
        return .TerminateNow
    }

    do {
        try managedObjectContext.save()
    } catch {
        let nserror = error as NSError
        // Customize this code block to include application-specific recovery steps.
        let result = sender.presentError(nserror)
        if (result) {
            return .TerminateCancel
        }

        let question = NSLocalizedString("Could not save changes while quitting. Quit anyway?", comment: "Quit without saves error question message")
        let info = NSLocalizedString("Quitting now will lose any changes you have made since the last successful save", comment: "Quit without saves error question info");
        let quitButton = NSLocalizedString("Quit anyway", comment: "Quit anyway button title")
        let cancelButton = NSLocalizedString("Cancel", comment: "Cancel button title")
        let alert = NSAlert()
        alert.messageText = question
        alert.informativeText = info
        alert.addButtonWithTitle(quitButton)
        alert.addButtonWithTitle(cancelButton)

        let answer = alert.runModal()
        if answer == NSAlertFirstButtonReturn {
            return .TerminateCancel
        }
    }
    // If we got here, it is time to quit.
    return .TerminateNow
}

有人可以帮助解决这个错误吗?我以后如何通过更好的迁移来处理这个问题?

【问题讨论】:

    标签: xcode swift macos core-data migration


    【解决方案1】:

    “找不到源存储的模型”表示您正在尝试进行模型迁移,但 Core Data 找不到旧版本的数据模型。要使迁移工作,您需要同时拥有旧版本和新版本。该错误表明您只有新版本。

    当您要更改数据模型并迁移数据时,您需要在项目中创建模型的新版本。您可以通过在 Xcode 中选择模型文件并使用“编辑器”菜单 -->“添加模型版本”来完成此操作。然后,您可以对新版本进行更改,同时保留旧版本。

    您需要恢复旧模型。最好的方法是回顾你的 SCM(可能是 git)提交并获取它。

    【讨论】:

    • 谢谢,我所做的只是从模拟器中删除应用程序并重建。它的工作:D.
    • @FarrasDoko 删除应用程序并不能解决主要问题。删除会清除 Store 并根据您的新模型重新创建。当您的应用程序已经上线时,您不能要求用户删除该应用程序。您需要针对这种情况进行适当的核心数据迁移
    • @user121095 是的,就我而言,我更改了实体条目(属性)的数据类型,这很糟糕。我应该添加具有新数据类型的新属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2011-05-30
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    相关资源
    最近更新 更多