将Core Data对象发送到“只读”Apple Watch App(Sending Core Data objects to “read only” Apple Watch App)

编程入门 行业动态 更新时间:2024-10-21 07:53:11
将Core Data对象发送到“只读”Apple Watch App(Sending Core Data objects to “read only” Apple Watch App)

我正在尝试开发一个watchOS 2应用程序以配合我的iOS应用程序。 iOS应用程序使用Core Data,而Apple Watch应用程序只是一个“只读”客户端,并显示来自iOS应用程序的数据。

我已经阅读了一些关于管理两个数据存储的事情,但这似乎有些过分。 我只想在启动时将数据传输到手表应用程序,然后如果手机上的内容发生变化则发送后台传输。

我的问题是如何将此信息最初发送到手表应用程序? 我认为我不能将实际的数据对象发送到手表应用程序。 我是否需要将对象转换为字典并通过WatchConnectivity API发送所有相关信息?

I am trying to develop a watchOS 2 app to go along with my iOS app. The iOS app utilizes Core Data, and the Apple Watch app is simply going to be a "read only" client and display the data from the iOS app.

I have read a few things regarding managing two data stores, but that seems to be overkill. I just want to transfer the data to the watch app on launch then send background transfer if things change on the phone.

My question is how do I send this information to the watch app initially? I don't think I can send the actual data objects to the watch app. Do I need to convert the objects to a dictionary and send all of the relevant information via the WatchConnectivity API?

最满意答案

你的方法听起来不错。 如果您不玩更改手表上的数据,那么在您的手表应用程序上镜像您的CoreData数据库确实会有点过分。

因此,使用应用程序上下文通过后台传输发送数据是正确的选择。 这只有一点需要注意:用于传输数据的updateApplicationContext方法只接受属性列表值的字典。 换句话说,您只能发送可以添加到属性列表的对象:

排列 字典 串 数据(NSData) 日期(NSDate) 整数 浮点值 布尔

因此,在发送之前,您必须将Core Data对象转换为仅包含这些类型的字典。

这是一种努力,但即使您想在手表端镜像数据库,也必须这样做,因为使用watchOS2您不能再使用App Groups来共享CoreData文件。

Your approach sounds good. Mirroring your CoreData database on your watch app would indeed be overkill if you don't play to change the data on the watch.

So using the Application Context to send the data via background transfer is the right choice. This has only one caveat: The updateApplicationContext method that you use to tranfer the data only accepts a dictionary of property list values. In other words, you can only send objects that you could add to a property list:

Array Dictionary String Data (NSData) Date (NSDate) Integer Floating-point value Boolean

So, you'll have to convert your Core Data objects to dictionaries that contain only those types, before you can send them.

That's a bit of an effort, but you would have to do that anyway even if you wanted to mirror your database on the watch side, because with watchOS2 you cannot use App Groups anymore to share CoreData files.

更多推荐

本文发布于:2023-08-06 04:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   对象   Apple   Data   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!