{"__v":27,"_id":"56730163fc60740d00be8ab7","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":["5787c913e516ba0e003046c3","57fce0fa0312b20e00ac6542"],"next":{"pages":[],"description":""},"createdAt":"2015-12-17T18:39:31.158Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"`NSOperation` instances have an internal state machine, which is shown in the diagram below.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/WxbFeoeKSzSl2BLI3XiX_nsoperation_state_machine.png\",\n        \"nsoperation_state_machine.png\",\n        \"662\",\n        \"434\",\n        \"#fc4c54\",\n        \"\"\n      ],\n      \"caption\": \"The state machine for how NSOperation works.\",\n      \"sizing\": \"full\"\n    }\n  ]\n}\n[/block]\nThe cancelled state can be entered to at any time, but it doesn't actually do anything as far as execution works. Subclasses of `NSOperation` should be mindful of checking `cancelled` appropriately such as before executing, and perhaps even periodically during execution.\n\n`Operation` extends this state machine to support primary features of [*observers*](doc:observers) and [*conditions](doc:conditions).\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/UP4IANjJTPWoBudDD7sT_operation_state_machine.png\",\n        \"operation_state_machine.png\",\n        \"670\",\n        \"890\",\n        \"#fc4c54\",\n        \"\"\n      ],\n      \"caption\": \"The state machine for Operation class, which adds observers and conditions.\",\n      \"sizing\": \"full\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Evaluating Conditions\"\n}\n[/block]\nThe *evaluating condition* state is entered after the operation's dependencies finish. During this stage the operation's conditions are evaluated asynchronously. Upon completion, the operation moves to the *ready*, while if there are no conditions to begin with, it goes directly to the *ready* state.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Operation Conditions\",\n  \"body\": \"Read about [Operation Conditions](doc:conditions) in more detail.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Executing\"\n}\n[/block]\nWhen an `NSOperation` becomes *ready* its `main()` function is invoked. Although not shown in the state machine, at this point an `Operation` subclass may have been cancelled or collected some errors, such as if conditions failed. In which case it moves to *finishing* without *executing*. Otherwise, it notifies observers that it did start, and `execute()` is called. This is the class which subclasses must override. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Finishing\"\n}\n[/block]\nOnce *work* has been done, the operation must finish. From within `execute()` it should call `finish()` which will move the operation to the *finishing* state. From this state, `Operation` calls `finished()` with any errors, then notifies any relevant observers that the operation did finish. These are opportunities for to perform actions when an operation finishes, either by internally by subclassing, or externally by observing. Then the operation moves to *finished*. There is no way to prevent and operation from transitioning from the *finishing* to *finished* states.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Operation Observers\",\n  \"body\": \"Read about [Operation Observers](doc:observers)  in more detail.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Cancelled\"\n}\n[/block]\nAt any point it is possible to call `cancel()` or `cancelWithError()`. When this happens, the operation moves to *cancelled*. If the state is further along than *ready*, the operation will call `finish()`.","excerpt":"","slug":"scheduling","type":"basic","title":"Scheduling"}
`NSOperation` instances have an internal state machine, which is shown in the diagram below. [block:image] { "images": [ { "image": [ "https://files.readme.io/WxbFeoeKSzSl2BLI3XiX_nsoperation_state_machine.png", "nsoperation_state_machine.png", "662", "434", "#fc4c54", "" ], "caption": "The state machine for how NSOperation works.", "sizing": "full" } ] } [/block] The cancelled state can be entered to at any time, but it doesn't actually do anything as far as execution works. Subclasses of `NSOperation` should be mindful of checking `cancelled` appropriately such as before executing, and perhaps even periodically during execution. `Operation` extends this state machine to support primary features of [*observers*](doc:observers) and [*conditions](doc:conditions). [block:image] { "images": [ { "image": [ "https://files.readme.io/UP4IANjJTPWoBudDD7sT_operation_state_machine.png", "operation_state_machine.png", "670", "890", "#fc4c54", "" ], "caption": "The state machine for Operation class, which adds observers and conditions.", "sizing": "full" } ] } [/block] [block:api-header] { "type": "basic", "title": "Evaluating Conditions" } [/block] The *evaluating condition* state is entered after the operation's dependencies finish. During this stage the operation's conditions are evaluated asynchronously. Upon completion, the operation moves to the *ready*, while if there are no conditions to begin with, it goes directly to the *ready* state. [block:callout] { "type": "info", "title": "Operation Conditions", "body": "Read about [Operation Conditions](doc:conditions) in more detail." } [/block] [block:api-header] { "type": "basic", "title": "Executing" } [/block] When an `NSOperation` becomes *ready* its `main()` function is invoked. Although not shown in the state machine, at this point an `Operation` subclass may have been cancelled or collected some errors, such as if conditions failed. In which case it moves to *finishing* without *executing*. Otherwise, it notifies observers that it did start, and `execute()` is called. This is the class which subclasses must override. [block:api-header] { "type": "basic", "title": "Finishing" } [/block] Once *work* has been done, the operation must finish. From within `execute()` it should call `finish()` which will move the operation to the *finishing* state. From this state, `Operation` calls `finished()` with any errors, then notifies any relevant observers that the operation did finish. These are opportunities for to perform actions when an operation finishes, either by internally by subclassing, or externally by observing. Then the operation moves to *finished*. There is no way to prevent and operation from transitioning from the *finishing* to *finished* states. [block:callout] { "type": "info", "title": "Operation Observers", "body": "Read about [Operation Observers](doc:observers) in more detail." } [/block] [block:api-header] { "type": "basic", "title": "Cancelled" } [/block] At any point it is possible to call `cancel()` or `cancelWithError()`. When this happens, the operation moves to *cancelled*. If the state is further along than *ready*, the operation will call `finish()`.