Advanced CloudKit (Part II): CKOperation, CKQueryOperation

Last week, we published an in-depth CloudKit tutorial on managing critical data. So, in this post, I will build on what I showed you last week by teaching you how to manage a large amount of data with CloudKit.

CloudKit and Big Data

In this second example, we are going to update many records. Let’s take a look at the case of a dictionary App. We have our local copy of the data, but when the user downloads the App from the App Store and launches it for the first time, it is possible that some of the records from the initial set of data have become outdated.

The technique we are going to use here is very similar to what we have done in the first example. But instead of fetching a single record, we are going to use a query operation to fetch a group of records at once.

This is an example of the code you would use to achieve that:

Or in Objective-C:

In the first method, we create a query with a predicate to fetch the records. We create a CKQueryOperation and pass it to the second method, along with the operation queue that we will use to execute it.

In the second method we setup the query:
- we assign the database where it will be executed;
- we set a block that will be executed for each record found, where we will process the changes and update the local copy of the data;
- we set a completion handler that will be executed when the operation finishes;
- we add the operation to the operation queue to execute it.

In the queryCompletionBlock, as you can see in the source code, you should handle any errors that may occur (see our previous post on CloudKit). Note that we are checking if a CKQueryCursor is provided. If it exists, that indicates that there are more results to fetch (because the operations are limited in size and number of records). If so, we use the cursor to initialize a new query operation and we use the same method to setup and execute it. This ensures that we will create and execute serialized query operations until we finally process all the records.

Pre-populate a CloudKit database

To populate our database of word definition changes, we are going to create a record for each of the word definitions that have changed and save it in iCloud using CloudKit. This time we are talking about lots of records, so doing it manually on the CloudKit dashboard is not the best solution. So, we are going to create another App, a Mac App, to do the work automatically.

In your project, create a new target and choose a Mac App. We need to enable CloudKit, as we did with our iOS App. Select the target that you have just created and follow the same steps, going to the Capabilities pane and switching on iCloud, and activating the CloudKit checkbox. This time we want to have access to the iCloud container of the iOS App, because we need to save the data in that container, so the iOS App can read the data from it. To do that, select Specify custom containers. A list of available containers will appear. Look for the container of your iOS App and select its checkbox.


Now we are ready to create a CKRecord for each of our word definitions. Instead of saving them one by one, we will create a CKModifyRecordsOperation and will save all of them at once. Use the following code as an example of how you can do that:

Or in Objective-C:

The first thing to note in the code is that this time, instead of using the default container, we are using a custom container, which points to the container that the iOS App will use later to read and update the records.

Then we create an instance of a CKRecord for each of our word definitions, and put them in an array. We create a CKModifyRecordsOperation and pass the array of records to save to it, and also we tell it to use the public database of the custom container. We also define a completion block that will be executed when the operation finishes. To execute the operation, we create a NSOperationQueue and add the operation to it.

In the completion handler, you should handle any error that can occur, because the operation can succeed, fail, or only save some of the records, but not all of them. There are limits to the number of records and size of the operations. For example, at this time, you can only save a maximum of 400 objects in one operation. If you try to save more objects the operation will fail with an error. If you need to save more objects, do it in batches, and control which objects have already been saved in the completion handler. In our code, we have set a completion handler that will be executed once, when the operation finishes. If you need more control over each of the records, you can also set a completion handler perRecordCompletionBlock that will execute once per each of the records.

Once you execute this code, you will see that, in the CloudKit Dashboard, a new record type has appeared. This is possible because we are still in the Development environment, and that makes it easy to create the database schema by code. Once you change to the Production environment, this will not be possible, and the record types and its fields will need to exist before you make operations against them, or they will fail.


Thanks for reading our in-depth CloudKit tutorials. CloudKit is a powerful technology that empowers iOS apps through data storage and access. I encourage you to continue investigating CloudKit, and using it to build powerful Apps for Apple devices. To learn more, go to where you will find lots of excellent resources.

Keep Coding!!!

Vicente Vicens



(Visited 1,823 times, 2 visits today)