Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Core Data with watchOS 2.0

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!

like image 897
Becky Hansmeyer Avatar asked Jun 14 '15 20:06

Becky Hansmeyer


People also ask

Should I use Core Data iOS?

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.

When should I use Core Data?

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.

What does it mean to use Core Data Xcode?

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.


Video Answer


2 Answers

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.

like image 116
Derek Pollard Avatar answered Sep 28 '22 06:09

Derek Pollard


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.

like image 28
ccjensen Avatar answered Sep 28 '22 04:09

ccjensen