-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockObserver.swift
More file actions
41 lines (32 loc) · 1.27 KB
/
BlockObserver.swift
File metadata and controls
41 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s 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 `Operation`'s lifecycle.
*/
struct BlockObserver: OperationObserver {
// MARK: Properties
private let startHandler: (Operation -> Void)?
private let produceHandler: ((Operation, NSOperation) -> Void)?
private let finishHandler: ((Operation, [NSError]) -> Void)?
init(startHandler: (Operation -> Void)? = nil, produceHandler: ((Operation, NSOperation) -> Void)? = nil, finishHandler: ((Operation, [NSError]) -> Void)? = nil) {
self.startHandler = startHandler
self.produceHandler = produceHandler
self.finishHandler = finishHandler
}
// MARK: OperationObserver
func operationDidStart(operation: Operation) {
startHandler?(operation)
}
func operation(operation: Operation, didProduceOperation newOperation: NSOperation) {
produceHandler?(operation, newOperation)
}
func operationDidFinish(operation: Operation, errors: [NSError]) {
finishHandler?(operation, errors)
}
}