{"__v":7,"_id":"5674014947f7010d005bff1c","category":{"__v":7,"_id":"567400638565060d009a86fb","pages":["567400df22cd7e0d00fada7f","56740103f65d9c0d002e3d5e","5674010f47f7010d005bff1a","56740128f79ca90d00ad2f65","5674014947f7010d005bff1c","5674015622cd7e0d00fada81","5674016058c4890d00bb5558"],"project":"5672fc989996590d00c22c65","version":"5672fc989996590d00c22c68","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-18T12:47:31.547Z","from_sync":false,"order":2,"slug":"available-operations","title":"Available Operations"},"project":"5672fc989996590d00c22c65","user":"5654ea8be0d82b0d00ab5747","version":{"__v":7,"_id":"5672fc989996590d00c22c68","project":"5672fc989996590d00c22c65","createdAt":"2015-12-17T18:19:04.699Z","releaseDate":"2015-12-17T18:19:04.699Z","categories":["5672fc999996590d00c22c69","567301169d4c060d009da8b3","56730183547bee0d00997d1a","5673018a06b19d0d0010691b","567301b53054630d00fe9288","567400638565060d009a86fb","5674017bf79ca90d00ad2f67"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"2.5.0","version":"2.5.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-18T12:51:21.547Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"CloudKit is a framework which enables communication between Apple's CloudKit server and user's devices. It is highly asynchronous and error prone problem domain and the CloudKit engineers have chosen to base all of the functionality on `NSOperation` subclasses.\n\n`CloudKitOperation` is a composing operation similar to `ComposedOperation`. It's usage is to support executing `CKDatabaseOperation` subclasses. The `Operation` subclass supports setting the `CKDatabase` and executing the operation.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let fetch = CloudKitOperation(\\n    operation: CKFetchRecordsOperation(recordIDs: ids), \\n    database: db\\n)\\nfetch.addObserver(NetworkObserver())\\nqueue.addOperation(fetch)\",\n      \"language\": \"swift\",\n      \"name\": \"fetch record changes\"\n    }\n  ]\n}\n[/block]\nMost of the `CKDatabaseOperation` have block based APIs which perform work during execution. These are well suited to collect input data such as records, or identifiers, and then trigger new `Operation` subclasses. For example creating a CloudKit zone:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Assume we have an operation which persists some state\\nlet didCreateZoneOperation = DidCreateCloudKitZoneOperation(zone: recordZone)\\n\\n// Create the zone operation\\nlet createZoneOperation = CloudKitOperation(\\n    // This is the CKDatabaseOperation we want to execute\\n    operation: CKModifyRecordZonesOperation(recordZonesToSave: [recordZone], recordZoneIDsToDelete: nil), \\n    database: db)\\n\\n// Always give our operation's names\\ncreateZoneOperation.name = \\\"Create CloudKit Zone\\\"\\n\\n// Set the CloudKit completion block on the composed operation\\ncreateZoneOperation.operation.modifyRecordZonesCompletionBlock = { [weak operation = createZoneOperation] (saved, _, error) in\\n    if let error = error {\\n        // Handle errors..\\n    }\\n    else {\\n        // If there are no errors, produce the did create zone\\n        // operation.\\n        operation?.produceOperation(didCreateZoneOperation)\\n    }\\n}\\n\",\n      \"language\": \"swift\",\n      \"name\": \"create cloudkit zone\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Making a dedicated GroupOperation subclass\",\n  \"body\": \"The above sample code would be ideally suited to be inside a `GroupOperation` subclass. The intention being to encapsulate the creation of the zone, persistence of state etc.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Create an OperationCondition\",\n  \"body\": \"Going even further, such a `GroupOperation` would be ideal as the dependency operation for a condition. Such a condition can then be attached to other `Operation` subclasses which interact with the zone.\"\n}\n[/block]","excerpt":"","slug":"cloudkit-operation","type":"basic","title":"CloudKit Operation"}

CloudKit Operation


CloudKit is a framework which enables communication between Apple's CloudKit server and user's devices. It is highly asynchronous and error prone problem domain and the CloudKit engineers have chosen to base all of the functionality on `NSOperation` subclasses. `CloudKitOperation` is a composing operation similar to `ComposedOperation`. It's usage is to support executing `CKDatabaseOperation` subclasses. The `Operation` subclass supports setting the `CKDatabase` and executing the operation. [block:code] { "codes": [ { "code": "let fetch = CloudKitOperation(\n operation: CKFetchRecordsOperation(recordIDs: ids), \n database: db\n)\nfetch.addObserver(NetworkObserver())\nqueue.addOperation(fetch)", "language": "swift", "name": "fetch record changes" } ] } [/block] Most of the `CKDatabaseOperation` have block based APIs which perform work during execution. These are well suited to collect input data such as records, or identifiers, and then trigger new `Operation` subclasses. For example creating a CloudKit zone: [block:code] { "codes": [ { "code": "// Assume we have an operation which persists some state\nlet didCreateZoneOperation = DidCreateCloudKitZoneOperation(zone: recordZone)\n\n// Create the zone operation\nlet createZoneOperation = CloudKitOperation(\n // This is the CKDatabaseOperation we want to execute\n operation: CKModifyRecordZonesOperation(recordZonesToSave: [recordZone], recordZoneIDsToDelete: nil), \n database: db)\n\n// Always give our operation's names\ncreateZoneOperation.name = \"Create CloudKit Zone\"\n\n// Set the CloudKit completion block on the composed operation\ncreateZoneOperation.operation.modifyRecordZonesCompletionBlock = { [weak operation = createZoneOperation] (saved, _, error) in\n if let error = error {\n // Handle errors..\n }\n else {\n // If there are no errors, produce the did create zone\n // operation.\n operation?.produceOperation(didCreateZoneOperation)\n }\n}\n", "language": "swift", "name": "create cloudkit zone" } ] } [/block] [block:callout] { "type": "success", "title": "Making a dedicated GroupOperation subclass", "body": "The above sample code would be ideally suited to be inside a `GroupOperation` subclass. The intention being to encapsulate the creation of the zone, persistence of state etc." } [/block] [block:callout] { "type": "success", "title": "Create an OperationCondition", "body": "Going even further, such a `GroupOperation` would be ideal as the dependency operation for a condition. Such a condition can then be attached to other `Operation` subclasses which interact with the zone." } [/block]