{"__v":19,"_id":"567301539d4c060d009da8b4","category":{"__v":3,"_id":"567301169d4c060d009da8b3","pages":["5673014406b19d0d00106919","567301539d4c060d009da8b4","56730163fc60740d00be8ab7"],"project":"5672fc989996590d00c22c65","version":"5672fc989996590d00c22c68","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-17T18:38:14.940Z","from_sync":false,"order":0,"slug":"getting-started","title":"Getting Started"},"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":["570f4f6d986c040e008f7ad4","5787c86b5286030e00589d2f"],"next":{"pages":[],"description":""},"createdAt":"2015-12-17T18:39:15.482Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"Creating a custom operation is easy. Here is a simple example. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import Operations\\n\\nclass MyFirstOperation: Operation {\\n    override func execute() {\\n        print(\\\"Hello World\\\")\\n        finish()\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThis is a contrived example, but the important points are\n1. Subclass `Operation`\n2. Override `execute()`, but *do not* call super.\n3. Always call `finish()` when the work is complete.\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"title\": \"Finishing\",\n  \"body\": \"If the operation does not call `finish()` or `finishWithError()` it will never complete. This might block a queue from executing subsequent operations. Any operations which are waiting on the stuck operation will not start.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Executing the operation\"\n}\n[/block]\nTo execute the operation, add it to an `OperaitonQueue`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let queue = OperationQueue()\\nlet operation = MyFirstOperation()\\nqueue.addOperation(operation)\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nIf the queue is not suspended, operations will be executed as soon as they become ready and the queue has capacity.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Apply best practices\"\n}\n[/block]\nThe operation is a class, and so object orientated programming best practices apply. For example pass known dependencies into the operation's initializer: \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class Greeter: Operation {\\n\\n    let personName: String\\n    \\n    init(name: String) {\\n        self.personName = name\\n        super.init()\\n        name = \\\"Greeter Operation\\\"\\n    }\\n    \\n    override func execute() {\\n        print(\\\"Hello \\\\(personName)\\\")\\n        finish()\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Give your operation a name\",\n  \"body\": \"`NSOperation` has a `name` property. It can be handy to set this for debugging purposes.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Responding to Cancellation\",\n  \"body\": \"This example has been kept particularly simple to get some key points across. But, check out the page on cancellation for a better implementation.\"\n}\n[/block]","excerpt":"","slug":"myfirstoperation","type":"basic","title":"MyFirstOperation"}
Creating a custom operation is easy. Here is a simple example. [block:code] { "codes": [ { "code": "import Operations\n\nclass MyFirstOperation: Operation {\n override func execute() {\n print(\"Hello World\")\n finish()\n }\n}", "language": "swift" } ] } [/block] This is a contrived example, but the important points are 1. Subclass `Operation` 2. Override `execute()`, but *do not* call super. 3. Always call `finish()` when the work is complete. [block:callout] { "type": "danger", "title": "Finishing", "body": "If the operation does not call `finish()` or `finishWithError()` it will never complete. This might block a queue from executing subsequent operations. Any operations which are waiting on the stuck operation will not start." } [/block] [block:api-header] { "type": "basic", "title": "Executing the operation" } [/block] To execute the operation, add it to an `OperaitonQueue`. [block:code] { "codes": [ { "code": "let queue = OperationQueue()\nlet operation = MyFirstOperation()\nqueue.addOperation(operation)", "language": "swift" } ] } [/block] If the queue is not suspended, operations will be executed as soon as they become ready and the queue has capacity. [block:api-header] { "type": "basic", "title": "Apply best practices" } [/block] The operation is a class, and so object orientated programming best practices apply. For example pass known dependencies into the operation's initializer: [block:code] { "codes": [ { "code": "class Greeter: Operation {\n\n let personName: String\n \n init(name: String) {\n self.personName = name\n super.init()\n name = \"Greeter Operation\"\n }\n \n override func execute() {\n print(\"Hello \\(personName)\")\n finish()\n }\n}", "language": "swift" } ] } [/block] [block:callout] { "type": "success", "title": "Give your operation a name", "body": "`NSOperation` has a `name` property. It can be handy to set this for debugging purposes." } [/block] [block:callout] { "type": "warning", "title": "Responding to Cancellation", "body": "This example has been kept particularly simple to get some key points across. But, check out the page on cancellation for a better implementation." } [/block]