Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kurt Frey
Kurt Frey

Posted on

Wrapping my head around CoreData and CloudKit

This blog post was also published onhttps://www.nitricware.com

If I got anything wrong, please leave a comment!

I like to understand things that wrapper and APIs like to obfuscate. CoreData especially in combination with CloudKit was very overwhelming for me. And it still is. However I could clear some things up for me.

Since I preferred Apple doing some heavy lifting, posts and tutorials that "built the whole Core Data Stack from scratch" confused me. I wanted to understand and expand what was already there.

Prepare your app for CoreData in iCloud

  1. Create a new application and check "use CoreData"and "host in iCloud"

OR(assuming the app already uses CoreData, because if not you'd have to add the wholePersistenceController)

  1. ReplaceNSPersitentContainer inPersistenceController withNSPersitentCloudKitContainer
  2. Addcontainer.viewContext.automaticallyMergesChangesFromParent = true inPersistenceController.init() as the last line
  3. In "Signing & Capabilities" addfor each target:
    1. Background Modes: Remote Notifications
    2. iCloud: CloudKit and activate the checkbox next to the container for your app (create a new one with+ if needed) - you could also use an existing one and share data between apps.

You're done.

Attention: iCloud sync in simulator is wonky at best. Use physical devices.

What is an iCloud container?

An iCloud container simply put is like a directory in iCloud where all the cloud stuff of your app reside. You're using anNSPersitentCloudKitContainer? It's synced to the iCloud container. Using multiple? A single one with multiple configurations? They're all there.

The iCloud container is like the sandbox of the app on the device but in the cloud.

What is aNSPersistentContainer

AnNSPersistentContainer is a class that handles all your interaction with a database (or multiple database files, see: configurations).

What is anNSPersistentCloudKitContainer

AnNSPersistentCloudKitContainer also handles all your interaction with a local database and it also handles syncing it to and from the cloud.

Multiple persistent container

What?

Usually there's a file calledPersistence.swift in your project. Or yourPersistenceController class is created elsewhere.

ThisPersistenceController instantiates a persistent container that connects to a.sqlite-database (which is defined by a.xcdatamodeld file) in your project. You reference it by name:NSPersistentContainer(name: "MyDataModel") - you need aMyDataModel.xcdatamodeld for that to work.

You could copy the whole code and instantiate a second container referencing another file.

AnNSPersistentCloudKitContainer would then sync both persistent containers (read: files) to your iCloud container (read: sandbox).

Conveniently it would simply sync it to the first iCloud container in the list of iCloud containers checked in the iCloud capability.

Why?

You could want to separate topics in different databases not just different tables. For clarity or so. I.e. an app that stores your refrigerator content and your car parts. Similar database structure but different enough that two database files make sense.

If you'd want to access a private and a public part of database in an iCloud container, you'd have to useNSPersistentCloudKitContainerOptions and configurations.

Of course you could use could use configurations for that silly use case above too.

UseNSPersistentCloudKitContainerOptions and configurations

Say you want to connect aNSPersistentCloudKitContainer to a different iCloud container than the first in the list you marked with a checkmark.

Or you'd like to connect the public and private records in the first (or any other) iCloud container in said list.

That's whatNSPersistentCloudKitContainerOptions is for.

// point to the database fileletpublicDescription=NSPersistentStoreDescription(url:url!.appendingPathComponent("public.sqlite"))// point to the container in the cloudletpublicOptions=NSPersistentCloudKitContainerOptions(containerIdentifier:"iCloud.com.nitricware.Aphrodite")// point to the section of the container in the cloudpublicOptions.databaseScope=.public// link the NSPersistentCloudKitContainerOptions to NSPersistentStoreDescriptionpublicDescription.cloudKitContainerOptions=publicOptions// point to the configuration in your .xcdatamodeldpublicDescription.configuration="Public"// enable uploading already existent entries in your databasepublicDescription.setOption(trueasNSNumber,forKey:NSPersistentHistoryTrackingKey)// this option enables auto refreshpublicDescription.setOption(trueasNSNumber,forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey)
Enter fullscreen modeExit fullscreen mode

Further Reading:https://patmalt.medium.com/end-to-end-encryption-and-cloudkit-with-coredata-part-1-67bfbe467bc

Applying it to Core Data without cloud sync

Simply use aNSPersistentContainer and omit the lines below from the code above.

letpublicOptions=NSPersistentCloudKitContainerOptions(containerIdentifier:"iCloud.com.nitricware.Aphrodite")publicOptions.databaseScope=.public
Enter fullscreen modeExit fullscreen mode

Is there more to it?

Yes. Loads. It's a highly complex topic. I don't understand all of it yet.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Digital Healthcare engineer from Austria. Physiotherapist, Radio Amateur.
  • Location
    Austria
  • Education
    BSc Health Studies, MSc Engineering, MBA Healthcare Management
  • Joined

More fromKurt Frey

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp