【问题标题】:How to load preexist sqlite file with MagicalRecord?如何使用 MagicalRecord 加载预先存在的 sqlite 文件?
【发布时间】:2013-05-07 00:07:36
【问题描述】:

我已经使用我在网上找到的一些脚本预先构建了一个特定于 Core Data 的 sqlite 文件。 然后我尝试通过插入将该文件用作我的默认 sqlite 数据库

[MagicalRecord setupCoreDataStackWithStoreNamed:@"database.sqlite"];

在 didFinishLaunchingWithOptions 方法中。但是当我启动我的应用程序时,它似乎创建了一个空数据库。我只是想知道使用 MagicalRecord 加载预建 sqlite 数据库的正确方法是什么?

【问题讨论】:

    标签: ios sqlite magicalrecord


    【解决方案1】:

    确保您之前的数据存储在 {App Folder}/Library/Application Support/{App Name}/database.sqlite。如果您的商店不存在,MagicalRecord(实际上是 Core Data)将为您创建一个新的。

    如果您希望您的商店在其他地方,您可以使用以下方法:

    [MagicalRecord setupCoreDataStackWithStoreAtURL:<NSURL to your store>];
    

    希望这会有所帮助。

    【讨论】:

    • 当我调用 setupCoreDataStackWithStoreAtURL 时,它说选择器“setupCoreDataStackWithStoreAtURL”没有已知的类方法。好像找不到这个方法。
    • 查看标题以了解确切的方法
    • 如何将我的 sqlite 文件写入 /Library/Application Support 文件夹?因为我从某个地方读到我们不允许在该文件夹中写入。而且我找不到定义替代 sqlite 文件的方法。是 setupCoreDataStackWithAutoMigratingSqliteStoreNamed 吗?
    【解决方案2】:

    我最终学习了this 的教程。主要方法是persistentStoreCoordinator。此外,在 didFinishLaunchingWithOptions 方法中,而不是

    PatientTableViewController *controller = (PatientTableViewController *)masterNavigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
    

    我放了

    [self managedObjectContext];
    [MagicalRecord setupCoreDataStackWithStoreNamed:@"database.sqlite"];
    

    还必须记住将 sqlite 文件和数据模型文件 (.xcdatamodeld) 更改为我拥有的文件。

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 2020-10-23
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多