【问题标题】:Core Data (Magical Record) + WatchKit Extension + Cocoa Touch FrameworkCore Data (Magical Record) + WatchKit Extension + Cocoa Touch 框架
【发布时间】:2015-09-14 16:08:13
【问题描述】:

这就是我要做的:

我现在有

  • 一个 iPhone 应用程序
  • WatchKit 扩展
  • 包含我所有共享类的 Cocoa Touch 框架

我想要实现的目标是拥有一个在我的 iPhone 应用和 WatchKit 扩展之间共享的持久存储(核心数据)。

这就是我到目前为止所做的事情

  • 创建一个应用组以拥有一个共享容器。
  • 将核心数据模型 (Model.xcdatamodeld) 添加到我的 Cocoa Touch 框架中。
  • 在此模型中创建了一个实体
  • 为此实体创建了一个 NSMangedObject 子类并添加到我的 Cocoa Touch 框架中
  • 在我的 Cocoa Touch 框架中添加了一个 DataManager 类

我的 DataManager 中的初始化程序如下所示

public init() {
    let sharedContainerURL: NSURL? = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.company.Project")
    if let sharedContainerURL = sharedContainerURL {
        let storeURL = sharedContainerURL.URLByAppendingPathComponent("Model.sqlite")
        MagicalRecord.setupCoreDataStackWithAutoMigratingSqliteStoreAtURL(storeURL)
        let station: Station? = Station.MR_createEntity()
    }
}

我遇到的问题

当我从 iPhone 应用的 AppDelegate 初始化 DataManager 时,没有发生崩溃,但 station 将为零。

当我用let stations: [Station]? = Station.MR_findAll() as? [Station] 替换最后一行时,应用程序崩溃并显示以下错误:A fetch request must have an entity.

我在 GitHub 上搜索了所有 SO 和 Magical Records 问题,但找不到任何东西可以推动我朝着正确的方向前进。非常感谢所有帮助。

【问题讨论】:

标签: swift core-data watchkit magicalrecord cocoa-touch


【解决方案1】:

感谢Leo Natan 的评论,我现在意识到我应该将我的核心数据存储在我的 iPhone 应用程序的沙箱以及我的 WatchKit 应用程序的沙箱中。而不是在共享容器中,就像我尝试的那样。

在为 Watch OS 2 构建时,我将能够使用 WatchKit 连接框架使两个数据库保持同步。同时,我可以使用MMWormhole 之类的解决方案来实现相同的目标。

【讨论】:

    【解决方案2】:

    我已经在我的实时应用中使用手表和 iPhone 完成了我的任务。我不需要 2 家商店。 MMWormhole 是帮助双方即时回电的好方法。我也处理了handleWatchKitExtensionRequest.

    的手表事件

    同步正确且运行良好。

    我关注了这个论坛。 - http://www.makeandbuild.com/blog/post/watchkit-with-shared-core-data

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-20
      • 2014-09-27
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多