【问题标题】:Why is my app crashing when trying to save to core data? [duplicate]为什么我的应用程序在尝试保存到核心数据时会崩溃? [复制]
【发布时间】:2015-04-04 21:18:14
【问题描述】:

我正在尝试将我的应用程序保存到 AppDelegate.swift 中的核心数据,但在退出应用程序时,它会在控制台中出现此日志并崩溃:

2015-04-04 22:04:38.043 myapp[14882:714850] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/kiancross/Library/Developer/CoreSimulator/Devices/59D353AF-8C56-42D6-8B35-9F51FE04D1BC/data/Containers/Data/Application/66D76678-B819-4B01-8172-6324BB5D2E7B/Documents/myappsqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7fc453dcb120 {metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        SavedData = <f13dc99c 2ebf5dd2 a89322cb 537a0f94 361477df 04077315 5954c0b5 afd3f9a1>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "E748E18D-F898-4AC1-96A4-7F8F1925D369";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
    metadata =     {
        NSPersistenceFrameworkVersion = 519;
        NSStoreModelVersionHashes =         {
            SavedData = <f13dc99c 2ebf5dd2 a89322cb 537a0f94 361477df 04077315 5954c0b5 afd3f9a1>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "E748E18D-F898-4AC1-96A4-7F8F1925D369";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
2015-04-04 22:04:38.047 myapp[14882:714850] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x7fc453dcce00 {NSLocalizedDescription=Failed to initialize the application's saved data, NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSUnderlyingError=0x7fc453db2360 "The operation couldn’t be completed. (Cocoa error 134100.)"}), Optional([NSLocalizedDescription: Failed to initialize the application's saved data, NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7fc453dcb120 {metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        SavedData = <f13dc99c 2ebf5dd2 a89322cb 537a0f94 361477df 04077315 5954c0b5 afd3f9a1>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "E748E18D-F898-4AC1-96A4-7F8F1925D369";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}])

在我的AppDelegate.swift 中触发此错误的代码是:

func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        let managedContext = appDelegate.managedObjectContext!

        let entity =  NSEntityDescription.entityForName("SavedData", inManagedObjectContext: managedContext)


        let managedObject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext)

        managedObject.setValue(sharedData.jsonUrl, forKey: "jsonUrl")

        var error: NSError?
        if !managedContext.save(&error) {
            println("Could not save \(error), \(error?.userInfo)")
        }

    }

这是在我的 CoreData 模型中;您可以在下面看到一个屏幕截图:

如您所见,它拥有应有的一切。

我的问题是我的代码的哪一部分导致了这次崩溃?我按照 this 教程将 CoreData 实施到应用程序中,我几乎可以肯定我已经尽可能地按照它来满足我的应用程序需求。

谢谢,

【问题讨论】:

  • this问题/答案。最简单的解决方法就是删除应用程序并重新运行。

标签: swift core-data crash


【解决方案1】:

我不太确定,但您的错误的最后一行显示:reason=The model used to open the store is incompatible with the one used to create the store}]) 这意味着 coredata 的数据模型与您使用的不同。

只需删除应用程序(长按应用程序图标并删除它)并再次运行它,至少可以解决该问题。

更好的解释是here

从模拟器中移除应用并对您的项目执行清理。那应该可以解决这些问题。删除应用程序时,请确保您没有在调试器中运行,否则它实际上不会正确删除它。

如果您想确定它已消失,请检查此目录 Users/INSERT_YOUR_USER_HERE/Library/Application Support/iPhone Simulator/ 以获取您正在运行的版本下的应用文件夹。

注意:这仅用于开发。对于生产,您需要实施某种迁移。谷歌“核心数据迁移”,轻量级迁移是最简单的。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多