How to Deal With iOS Local Storage and iCloud, to Have Synchronized Data
By: Danny Santoso
Tags: Core Data, Cloudkit, iOS, iOS App Development, Swift

Before we start to hands-on about how to implement core data as local storage that was synchronized with the data that was in iCloud. First, I want to enlighten you about the framework that we will use, which are core-data and cloud-kit.

“Cloud-Kit is a framework that is provided by Apple that store structured app and user data in iCloud containers that all users of your app can share.” “Core-data is a framework that is provided by Apple 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.”

by those explanations, I hope you already understand those 2 different frameworks, to be clear I will concise those explanations.

Core-data is a framework that stores your user data in the local storage of their device, Core Data isn’t a database but it’s a framework for managing an object graph, and then Cloud-Kit is a framework to store your user data in the iCloud.

Before we hands-on, I hope you have been subscribed Apple Developer Program. Because, if we want to use all the utilities in Cloud-Kit, we need to subscribe to Apple Developer Program. but if you just want to store your user data in their device local storage you don’t need to subscribe Apple Developer Program.

If you want to subscribe Apple Developer Program you can use this link.

Hands-on :

First, when you create a new project you need to checklist the core-data checkbox, but if you already have a project that hasn’t checklist the core-data checkbox you can follow these several steps.

Double Click on the folder name in the navigator area, choose a new file, and then choose the Data Model file.

After you have successfully created the Data Model file, go to the AppDelegate.swift, and copy-paste the code below.

Don’t forget to import Core Data and change the NSPersistentContainer name into the name file of your Data Model that you have been created. Follow this step if you haven’t checklist the Core Data checkbox in the initial when creating a new project.

After you have copy-paste the code above, now you can go to the Data Model file that you have been created before, and it will be displayed like this below.

To add a new model in your Core Data you can click the Add Entity button, and then type the name of your model. and if you want to add the attribute within your model, first you need to click your model that you want to add the attribute and then click + button in the below attribute table.

If you click the dropdown of the attribute type, you will see there are many types that you can use to define your attribute type in your model.

If you want to store the image into your Core Data model as an attribute, you can choose binary data as your attribute type for your image attribute.

After you have created Data model and their attributes, you can create the extension of each Data model to utilities your Data model to have several function like save, update, delete, and read their data.

Before we do those things, we need to create a new swift file to type those several function into the extension of each Data model / entity. After you have created a new file for contain all the function to utilitize CRUD operation, You can copy-paste code below.

In the code above, line 5–16 is the function to fetch all the data that was stored in the User entity/model, and if you see in line 8, the data that will be displayed were sorted by the date attribute within that entity. In the example of the code above, I want to sort the data reversed from the ascending, so it will display the data from the newest date — to the latest date.

line 18–33 is the function to fetch data by the predicate that you was intended, in the example of the code above, I want to fetch the user data by the name that I was inputted.

line 36–50 is the function to save the data into the Core Data, and line 52–64 is the function to update the data.

Before to use those functions you can copy-paste the code below, this function is an extension function for UIViewController, this function is used to simplify to get NSManagedObjectContext to be inputted in the function that needed to save, update, delete, and fetch data in Core Data Model.

After you have copy-paste the code above, now you can easily call the functions in the extension of your data model/entity, like this code below.

Here is the full code that implements those functions, but you need to add first the table view and its cell using xib:

ViewController.swift

TableViewCell.swift

After you have followed all those steps, now it’s time to sync Core-Data into iCloud or Cloud-Kit

First, click your .xcodeproj in the navigator area, and then click signing & capabilities, then click the + button in the Xcode toolbar, and choose iCloud. Now your screen will look like this.

In the iCloud menu services, checklist the Cloudkit checkbox, and if you don’t have any container to store your data you can click the + button in the containers, and then type your container name.

After you have followed those step, your container will automatically be created, to access your containers you can click this link.

Now you need to modify your AppDelegate.swift to code below.

in the code above we just doing a little bit of change, we just changing NSPersistentContainer to NSPersistentCloudKitContainer.

And if your entity / model has an image or another binary data type that needed to be saved in the cloud kit (external storage), you need to checklist the Store in External Record File checkbox of your image attribute or binary data type attribute.

When you access your container, it will be displayed like this, you may be asked why I can’t query record my data? first, you need to enable the queryable of your recordName, in the Scheme menu.

After you click Scheme Menu, it will be displayed like this.

Then, click Edit Indexes, add index recordName, set index Type of recordName to Queryable, and Save Changes.

Now back to Data Menu, and you already able to query records, and it will be look like this.

Just to make sure, all the user data that was in Core Data that was sync to the CloudKit cannot be seen by us, because it was been saved in the private container, we only can see our data that was been saved by using our apple id that had the same id with the developer account, so we can’t see other user data.

Here is the full code of this article, disclaimer you can’t run this file because it using my CloudKit container, so my advice is, change the container name to your own container.

References :