{"_id":"56730268cae8410d008a10b6","__v":4,"category":{"_id":"56730183547bee0d00997d1a","project":"5672fc989996590d00c22c65","__v":6,"pages":["5673025a30d9800d003a2ea8","56730268cae8410d008a10b6","5673027030d9800d003a2eaa","567344caf65d9c0d002e3c89","56735274f65d9c0d002e3c9b","5676c05fa7d01d0d0034fa79"],"version":"5672fc989996590d00c22c68","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-17T18:40:03.307Z","from_sync":false,"order":1,"slug":"beginners","title":"Operation Features"},"version":{"_id":"5672fc989996590d00c22c68","__v":7,"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"},"project":"5672fc989996590d00c22c65","user":"5654ea8be0d82b0d00ab5747","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-17T18:43:52.108Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"Observers are attached to a single `Operation`. They receive callbacks when operation events occur. There are currently four events \n[block:parameters]\n{\n  \"data\": {\n    \"0-0\": \"`OperationDidStartObserver`\",\n    \"0-1\": \"Triggered directly before the operation's `execute` method is invoked.\",\n    \"1-0\": \"`OperationDidCancelObserver`\",\n    \"1-1\": \"Triggered if and when the operation is cancelled.\",\n    \"2-0\": \"`OperationDidProduceOperationObserver`\",\n    \"2-1\": \"Triggered if and when the operation produces another operation.\",\n    \"3-0\": \"`OperationDidFinishObserver`\",\n    \"3-1\": \"Triggered directly before the operation moves to the finished state.\",\n    \"h-0\": \"Protocol\",\n    \"h-1\": \"Event\"\n  },\n  \"cols\": 2,\n  \"rows\": 4\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Adding an observer\"\n}\n[/block]\nAdd an observer before the operation becomes ready. If possible setup observers before adding the operation to a queue.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"operation.addObserver(StartedObserver { op in \\n    print(\\\"Lets go!\\\")\\n})\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n`StartedObserver`, `CancelledObserver`, `ProducedObserver` and `FinishedObserver` are all initialized with a block which is executed when the observer is trigger for its event. In the example above, the block would be executed just before `operation` has `execute` called.\n\nIf it is necessary to perform side effects when an operation starts *and* finishes, add a `StartedObserver` and a `FinishedObserver` there is no restriction on the number of observers.\n\nThe framework also includes `BackgroundObserver`, `TimeoutObserver`, and `NetworkObserver`.","excerpt":"Observers watch Operations","slug":"observers","type":"basic","title":"Observers"}

Observers

Observers watch Operations

Observers are attached to a single `Operation`. They receive callbacks when operation events occur. There are currently four events [block:parameters] { "data": { "0-0": "`OperationDidStartObserver`", "0-1": "Triggered directly before the operation's `execute` method is invoked.", "1-0": "`OperationDidCancelObserver`", "1-1": "Triggered if and when the operation is cancelled.", "2-0": "`OperationDidProduceOperationObserver`", "2-1": "Triggered if and when the operation produces another operation.", "3-0": "`OperationDidFinishObserver`", "3-1": "Triggered directly before the operation moves to the finished state.", "h-0": "Protocol", "h-1": "Event" }, "cols": 2, "rows": 4 } [/block] [block:api-header] { "type": "basic", "title": "Adding an observer" } [/block] Add an observer before the operation becomes ready. If possible setup observers before adding the operation to a queue. [block:code] { "codes": [ { "code": "operation.addObserver(StartedObserver { op in \n print(\"Lets go!\")\n})", "language": "swift" } ] } [/block] `StartedObserver`, `CancelledObserver`, `ProducedObserver` and `FinishedObserver` are all initialized with a block which is executed when the observer is trigger for its event. In the example above, the block would be executed just before `operation` has `execute` called. If it is necessary to perform side effects when an operation starts *and* finishes, add a `StartedObserver` and a `FinishedObserver` there is no restriction on the number of observers. The framework also includes `BackgroundObserver`, `TimeoutObserver`, and `NetworkObserver`.