{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Observers","type":"basic","slug":"observers","excerpt":"Observers watch Operations","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`.","updates":[],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56730268cae8410d008a10b6","__v":4,"category":{"sync":{"isSync":false,"url":""},"pages":["5673025a30d9800d003a2ea8","56730268cae8410d008a10b6","5673027030d9800d003a2eaa","567344caf65d9c0d002e3c89","56735274f65d9c0d002e3c9b","5676c05fa7d01d0d0034fa79"],"title":"Operation Features","slug":"beginners","order":1,"from_sync":false,"reference":false,"_id":"56730183547bee0d00997d1a","project":"5672fc989996590d00c22c65","__v":6,"createdAt":"2015-12-17T18:40:03.307Z","version":"5672fc989996590d00c22c68"},"createdAt":"2015-12-17T18:43:52.108Z","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"},"githubsync":"","project":"5672fc989996590d00c22c65","user":"5654ea8be0d82b0d00ab5747"}

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`.