Observers
Observers watch Operations
Observers are attached to a single Operation
. They receive callbacks when operation events occur. There are currently four events
Protocol | Event |
---|---|
OperationDidStartObserver | Triggered directly before the operation's execute method is invoked. |
OperationDidCancelObserver | Triggered if and when the operation is cancelled. |
OperationDidProduceOperationObserver | Triggered if and when the operation produces another operation. |
OperationDidFinishObserver | Triggered directly before the operation moves to the finished state. |
Adding an observer
Add an observer before the operation becomes ready. If possible setup observers before adding the operation to a queue.
operation.addObserver(StartedObserver { op in
print("Lets go!")
})
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
.
Updated less than a minute ago