swift Swift - CloudKit - 预填充数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - CloudKit - 预填充数据库相关的知识,希望对你有一定的参考价值。

See: https://www.invasivecode.com/weblog/advanced-cloudkit-ii

01. 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 
OS App and select its checkbox.

02. 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:

func uploadWordDefinitionChangesToCloudKit() {
    // Get a reference to the public database of the shared container
    let publicDatabase = CKContainer(identifier: "iCloud.com.yourdomain.YourAwesomeApp").publicCloudDatabase
 
    // Create an array of CKRecord instances to upload
    var recordsToUpload = [CKRecord]()
    for wordDefinition in self.wordDefinitionsToUpload() {
        let recordId = CKRecordID(recordName: wordDefinition.identifier)
        let record = CKRecord(recordType: "WordDefinitions", recordID: recordId)
        record["word"] = wordDefinition.word
        record["definition"] = wordDefinition.definition
        recordsToUpload.append(record)
    }
 
    // Create a CKModifyRecordsOperation operation
    let uploadOperation = CKModifyRecordsOperation(recordsToSave: recordsToUpload, recordIDsToDelete: nil)
 
    uploadOperation.atomic = false
    uploadOperation.database = publicDatabase
 
    // Assign a completion handler
    uploadOperation.modifyRecordsCompletionBlock = { (savedRecords: [CKRecord]?, deletedRecords: [CKRecordID]?, operationError: NSError?) -> Void in
        guard operationError==nil else {
            // Handle the error
            return
        }
        if let records = savedRecords {
            for record in records {
                // Mark the word definition as uploaded so is not included in the next batch
                self.markWordDefinitionAsUploaded(record.recordID.recordName)
            }
        }
    }
 
    // Add the operation to an operation queue to execute it
    NSOperationQueue().addOperation(uploadOperation)
}
 
 
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.

以上是关于swift Swift - CloudKit - 预填充数据库的主要内容,如果未能解决你的问题,请参考以下文章

swift Swift - CloudKit - 获取用户ID

swift Swift - CloudKit - 推送通知

swift Swift - CloudKit共享

swift Swift - CloudKit - 维护本地缓存

swift Swift - CloudKit - 部署架构

swift Swift - CloudKit订阅 - 5