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!
The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.
Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.
Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With