{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"MyFirstOperation","type":"basic","slug":"myfirstoperation","excerpt":"","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]","updates":["570f4f6d986c040e008f7ad4","5787c86b5286030e00589d2f"],"order":2,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"567301539d4c060d009da8b4","__v":19,"githubsync":"","user":"5654ea8be0d82b0d00ab5747","version":{"version":"2.5.0","version_clean":"2.5.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5672fc999996590d00c22c69","567301169d4c060d009da8b3","56730183547bee0d00997d1a","5673018a06b19d0d0010691b","567301b53054630d00fe9288","567400638565060d009a86fb","5674017bf79ca90d00ad2f67"],"_id":"5672fc989996590d00c22c68","__v":7,"createdAt":"2015-12-17T18:19:04.699Z","project":"5672fc989996590d00c22c65","releaseDate":"2015-12-17T18:19:04.699Z"},"category":{"sync":{"isSync":false,"url":""},"pages":["5673014406b19d0d00106919","567301539d4c060d009da8b4","56730163fc60740d00be8ab7"],"title":"Getting Started","slug":"getting-started","order":0,"from_sync":false,"reference":false,"_id":"567301169d4c060d009da8b3","project":"5672fc989996590d00c22c65","version":"5672fc989996590d00c22c68","__v":3,"createdAt":"2015-12-17T18:38:14.940Z"},"createdAt":"2015-12-17T18:39:15.482Z","project":"5672fc989996590d00c22c65"}