核心数据与WatchKit和iOS应用程序 [英] Core Data with WatchKit and iOS App

查看:175
本文介绍了核心数据与WatchKit和iOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已被要求,但已收到无答案。 p>

我的iOS应用程序和我的WatchKit扩展共享一个应用程序组,其中Core Data存储仍然存在。 Core Data模型(简化)是一个Person对象,它具有Age属性。应用和扩展程序都可以随意更改年龄。



如果某人的值在手表上更改,则应用程序不会反映该值,除非我关闭并重新运行app。



如果我尝试将手表的年龄更改为20,并将手机的年龄更改为30,那么我会收到一个错误关于合并冲突,因为我没有使用最新版本的NSManagedObject在其中一个设备(无论我改变最后的年龄)。


解决方案

由于通知不会在不同的进程,你需要使用达尔文通知。您可以轻松地使用 openParentApplication:reply:方法将更新从Watch扩展程序发送到iOS应用程序,但苹果没有办法从iOS应用程序发送事件观看分机。



我建议您使用 MMWormhole (或者您自己实现达尔文通知),以便在Core Data对象更新时发送Darwin通知。 这里是详细介绍图书馆工作原理的文章。



如果您在Watch扩展中更新Core Data对象,则向iOS应用发送达尔文通知(或传递消息),告诉应用需要刷新Core Data对象。从iOS应用程序到Watch扩展,可以使用相同的方法。



此外,如果对象非常简单,那么CoreData可能会过度使用。你可以使用MMWormhole简单地缓存一个轻量级的字典,用于两端。我已经使用这种方法几个星期了,它的工作完美无瑕在两个方向。


This question has already been asked, but has received no answers.

My iOS app and my WatchKit extension share an App Group in which a Core Data store persists. The Core Data model (simplified) is a Person object that has an attribute for Age. Both the app and extension can change the age at will.

If the value for a person changes on the watch, the app does not reflect it unless I close and rerun the app. Same going the opposite direction.

If I try to change the age on the watch to 20, and change the age on the phone to 30, then I get an error having to do with merge conflicts, because I am not using the most up-to-date version of the NSManagedObject on one of the devices (whichever I change the age on last).

How can I make the two work together without any issues?

解决方案

Since notifications won't be picked up between the different processes, you need to use Darwin notifications instead. You could easily use the openParentApplication:reply: method to send updates from the Watch Extension to the iOS App, but Apple does not have a way to send events from the iOS App to the Watch Extension.

What I would suggest is for you to use MMWormhole (or you own implementation of Darwin notifications) to send a Darwin notification when the Core Data object has been updated. Here is an article detailing how the library works.

If you update a Core Data object in the Watch Extension, then send a Darwin notification (or pass a message) to the iOS App telling the app that it needs to refresh the Core Data object. The same approach can be used going the other direction, from the iOS App to the Watch Extension.

Additionally, if you objects are really simple, then CoreData could be overkill. You could use MMWormhole to simply cache a light-weight dictionary that gets used on both ends. I've been using this approach for a few weeks now and it works flawlessly in both directions.

这篇关于核心数据与WatchKit和iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆