Using Core Data with watchOS 2.0

五迷三道 提交于 2019-12-17 15:49:26

问题


I made an iOS/Apple Watch app with WatchKit 1.0 that uses a shared group container and a custom framework to access the same Core Data store. Now, in the transition guide for watchOS 2.0, Apple says this:

If your existing Watch app and iOS app use a shared group container or iCloud to exchange data, you must change the way you exchange that data in watchOS 2. Because the WatchKit extension now runs on Apple Watch, the extension must exchange data with the iOS app wirelessly. You can do that using an NSURLSession object or using the Watch Connectivity framework, which supports bidirectional communication between your iOS app and WatchKit extension.

How does this work with Core Data, though? Do I need to maintain two separate stores, one in the Documents folder on the watch and one on the phone, and use the Watch Connectivity framework to sync the changes? I see that watchOS 2.0 can access the Core Data framework (it's in the list of Available Technologies)...but I'm just not sure how that helps when the store is located on the phone. Any help/guidance is appreciated!


回答1:


Yes, you'll have to maintain two separate stores. If either side is a "read-only" client and the CoreData datastore is small and changes infrequently you could potentially use the transferFile WatchConnectivity API to transfer the whole store each time it changes.




回答2:


I recommend setting up sync using the user info background transfer to mirror each database operation in both directions (if required) after transferring the store on first run.

The WWDC Session 713 - Introducing Watch Connectivity covers the mechanisms required. Nice API they've put together.



来源:https://stackoverflow.com/questions/30834078/using-core-data-with-watchos-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!