我正在尝试开发一个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 BooleanSo, 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.
更多推荐
发布评论