Using Core Data with watchOS 2.0

7.3k views Asked by At

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!

2

There are 2 answers

5
ccjensen On BEST ANSWER

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
Derek Pollard On

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.