Tutorial: Using CoreData with iCloud synchro

You can get tutorial source from https://github.com/blastar/CoreDataWithiCloud/tree/master

I saw lot of tutorials about this, but almost none were working.

So start with project as usual, for simplicity we can use Single View Application, in project capabilities enable iCloud:

enable icloud

Next open your pch file (in my project CoreDataWithiCloud-Prefix.pch) and add #import <CoreData/CoreData.h> so it will look like:

Open AppDelegate.h and add:

below

Open AppDelegate.m and add:

below:

then still in AppDelegate.m just before:

add:

Then go to your ViewController.h and add some stuff so this file will look like:

now its time for ViewController.m, import your AppDelegate by adding #import “AppDelegate.h” at top, then add:

in viewDidLoad, and below this method add new ones:

As you can see at this point, XCode will mark Contacts as error, as we don’t have any entities yet. So its time to add some. Add new file to your project, and choose Core Data/Data Model:

Data Model

You can call it whatever you want, I called it “MyModel” so after saving you should see MyModel.xcdatamodeld file. Click it and you should see empty entities panel:

Entities

Now its time do add our entity, at top menu click Editor/Add entity (you can use button at bottom of panel) and new entity will be added. Rename it to “Contacts” and add new property. Call it “name” and change type to “String”, now your view should look like this:

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Entity

Now we need to create out class, make sure you have your entity highlighted on left pane, then in top menu choose File/New/File and choose NSManagedObject subclass:

subclass

On next screen make sure you have MyModel checked:

Entity

Click “next” and check your entity:

Entity

Then you will be prompted to choose folder, after that you should see Contacts.h and Contacts.m in your project. Now add #import “Contacts.h” to your ViewController.m file and XCode errors should be gone. Now at last you can compile and run on your device, you should see blank screen (as we did not added any controls) and in debug area something like this:

Now its time to test whole thing, stop app on first device, and run it on second one, output will be similiar as above. Then the most important part – on first device run you app again, then wait ca. 30s and on second device (still running via XCode – so you should see your debug area) you should see:

If you see such line, congratulations, you did your first iCloud-aware app.

But what just happened? On your second device you had your app running, and listening for NSPersistentStoreDidImportUbiquitousContentChangesNotification notification, when it comes, – (void)contentDidChange:(NSNotification *)notification method is called, in which first you merged all changes, and then they are displayed in debug. Since we only adding items iterating on changes[NSInsertedObjectsKey] will be enough, but I added all variants.

You can get tutorial source from https://github.com/blastar/CoreDataWithiCloud/tree/master

Leave a Reply