Skip to content

Latest commit

 

History

History
106 lines (84 loc) · 2.61 KB

Ready-to-use-observers.md

File metadata and controls

106 lines (84 loc) · 2.61 KB

Some ready observers to use in your project

BlockObserver

The BlockObserver is a way to attach arbitrary blocks to significant events in an AOperation's lifecycle.

let observer = 
BlockObserver(startHandler: { operation in
   		// do something on start
   	},
   	produceHandler: { operation, newOperation in
   	// do something on produce operation
}, finishHandler: { operation, errors
   // do something on finishing operation
})

There are some helper methods that used BlockObserver which are easy to use for observing AOperation lifecycle:

didStart

Call this method on operation to observe starting of operation.

operation
.didStart {
}
.add(to: queue)

didProduce

Call this method on operation to observe produceing a newOperation.

operation
. didProduce { newOperation in
}
.add(to: queue)

didFinish

Call this method on operation to observe finished of operation.

  • Note1: This method is additive. means that all your called closures will be execute.
  • Note2: This method is execute in Main thread. So you can call UI functions into the closure in safety.
operation
.didFinish { result in
}
.add(to: queue)

willFinish

Call this method on operation to observe finishing of operation. this closure is executed just before operation moves to finished state. Use this method if you really need to your code execute before operation moves to finished, otherwise use didFinish.

  • Note: This method will just execute the last called function. So consider this in use of this method.
operation
.willFinish { result, finish in
	// doing something
	finish
}
.add(to: queue)

TimeoutObserver

TimeoutObserver is a way to make an Operation automatically time out and cancel after a specified time interval.

operation
.observers(TimeoutObserver(5))
.add(to: queue)

NetworkObserver

An AOperationObserver that will cause the network activity indicatior to appear as long as the AOperation to which it is attached is executing.

operation
.observers(NetworkObserver())
.add(to: queue)

ReachabilityObserver

An observer that performs a very high-level reachability observing. Use this observer to react on reachablility change during the operation execution.

operation
.didChangeReachability { operation, connection
	//React to connection change
}