Advanced-NSOperations/Earthquakes/Operations/BlockObserver.swift

41 lines
1.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
This file shows how to implement the OperationObserver protocol.
*/
import Foundation
/**
The `BlockObserver` is a way to attach arbitrary blocks to significant events
in an `EarthquakeOperation`'s lifecycle.
*/
struct BlockObserver: EarthquakeOperationObserver {
// MARK: Properties
private let startHandler: ((EarthquakeOperation) -> Void)?
private let produceHandler: ((EarthquakeOperation, Operation) -> Void)?
private let finishHandler: ((EarthquakeOperation, [NSError]) -> Void)?
init(startHandler: ((EarthquakeOperation) -> Void)? = nil, produceHandler: ((EarthquakeOperation, Operation) -> Void)? = nil, finishHandler: ((EarthquakeOperation, [NSError]) -> Void)? = nil) {
self.startHandler = startHandler
self.produceHandler = produceHandler
self.finishHandler = finishHandler
}
// MARK: OperationObserver
func operationDidStart(operation: EarthquakeOperation) {
startHandler?(operation)
}
func operation(operation: EarthquakeOperation, didProduceOperation newOperation: Operation) {
produceHandler?(operation, newOperation)
}
func operationDidFinish(operation: EarthquakeOperation, errors: [NSError]) {
finishHandler?(operation, errors)
}
}