Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
@jazzedge
jazzedge / Swift - CloudKit Subscriptions - 5
Last active November 30, 2017 05:03
It’s inefficient for your app to repeat a query when the results are mostly the same as the last query. Instead, subscribe to record changes, and let the server run the query in the background. The server will notify your app of changes that interest the user or app. For example, if one user of your app is interested in artwork by a certain arti…
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/SubscribingtoRecordChanges/SubscribingtoRecordChanges.html
01. Save Subscriptions to the Database
In your code, create a subscription object specifying the record type, predicate, and types of changes you
want to be notified about. Then save the subscription object to the database.
To create and save a subscription
A. Create a predicate object.
@jazzedge
jazzedge / Swift - CloudKit - Using asset and location fields
Last active November 30, 2017 04:41
CloudKit provides field types specifically for storing large data files and for fetching records by location. Use these data types to leverage the performance improvements that CloudKit provides for this type of data. You can also fetch records by location. For example, display records on a map in a user-defined region.
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/AddingAssetsandLocations/AddingAssetsandLocations.html
01. Store Large Files in CloudKit
You can store large data files in CloudKit using the Asset field type. Assets are owned by the associated
record, and CloudKit handles garbage collection for you. CloudKit also efficiently uploads and downloads
assets.
In code, the Asset field type is represented by a CKAsset object. This code fragment sets an Asset field
in an Artwork record to a resource file.
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/CreatingaSchemabySavingRecords/CreatingaSchemabySavingRecords.html
Improve the user’s experience by verifying that the user is signed in to their iCloud account
before saving records. If the user is not signed in, present an alert instructing the user how
to enter their iCloud credentials and enable iCloud Drive.
Insert your code that saves records in the else clause below.
CKContainer.default().accountStatus(completionHandler: {(_ accountStatus: CKAccountStatus, _ error: Error?) -> Void in
if accountStatus == .noAccount {
To run your app in iOS Simulator, enter the iCloud credentials in iOS Simulator
before you select the simulator and click the Run button in Xcode. You need to
perform these steps for each iOS Simulator you select in the Scheme pop-up menu in Xcode.
To enter iCloud credentials in iOS Simulator
Choose Xcode > Open Developer Tool > iOS Simulator.
In iOS Simulator, choose Hardware > Home.
Launch the Settings app and click iCloud.
../Art/3_icloud_settings_2x.png
@jazzedge
jazzedge / Swift - CloudKit - Reference types in relationships
Last active November 30, 2017 04:48
Use reference fields in your schema to represent relationships between model objects; for example, to represent hierarchical data or indicate ownership.
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/CreatingaSchemabySavingRecords/CreatingaSchemabySavingRecords.html
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/AddingReferences/AddingReferences.html
You can use reference field types to represent both one-to-one and one-to-many relationships between
your model objects. In your code, a reference field is a CKReference object that encapsulates the
record ID for a target record and is added to the source record. To represent a one-to-one relationship
in your schema, add a reference field to the source record type.
To represent a one-to-many relationship between your model objects, it is more efficient if the reference
The table shows possible field types, as they appear in CloudKit Dashboard,
and their equivalent CloudKit framework classes.
Field Type
Class
Description
Asset
CKAsset
A large file that is associated with a record but stored separately
You’ll need an iCloud account to save records to a CloudKit container.
You’ll enter the credentials for this iCloud account on the device that you run your app.
If you don’t have an iCloud account, create one that you can use during development.
On your Mac, launch System Preferences and click iCloud. Click Create Apple ID under the
Apple ID text field and follow the instructions.
In development, when you run your app through Xcode on a simulator or a device, you need to
enter iCloud credentials to read records in the public database. In production, the default
permissions allow non-authenticated users to read records in the public database but do not
allow them to write records.
@jazzedge
jazzedge / Swift - CloudKit - Add or share containers to one or more Apps
Created November 30, 2017 04:18
Optionally, configure your app to use multiple containers or share a container with other apps. For example, you might use one app internally to create record types and records programmatically to return a database to a known state. This app needs to share the same container as the end-user app you are developing and testing. To do this, you ena…
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/EnablingiCloudandConfiguringCloudKit/EnablingiCloudandConfiguringCloudKit.html#//apple_ref/doc/uid/TP40014987-CH2-SW1
01. Add Containers to an App
Select an existing container ID used by another app or create a new one.
To add a container to an app
A. In the Capabilities pane under the iCloud settings, select “Specify custom containers.”
When you previously selected the CloudKit service, Xcode created a default container ID for your
@jazzedge
jazzedge / Swift - CloudKit - Unique User ID
Created November 29, 2017 21:10
Within the scope of an application’s cloud container, each user has a unique, application specific iCloud user ID and a user info record.
Within the scope of an application’s cloud container, each user has a unique, application
specific iCloud user ID and a user info record where the user ID is used as the record ID
for the user’s info record.
The record ID of the current user’s info record can be obtained via a call to the
fetchUserRecordID(completionHandler:) method of the container instance. Once the record
ID has been obtained, this can be used to fetch the user’s record from the cloud database:
container.fetchUserRecordID(completionHandler: {recordID,
See: https://www.whatmatrix.com/blog/a-guide-to-cloudkit-how-to-sync-user-data-across-ios-devices/
01. Creating a Custom Zone
CloudKit automatically creates a default zone for the private database. However, you can get
more functionality if you use a custom zone, most notably, support for fetching incremental record changes.
Since this is a first example of using an operation, here are a couple of general observations:
First, all CloudKit operations have custom completion closures (and many have intermediate closures,