Skip to content

Commit

Permalink
Merge pull request #31 from davedelong/master
Browse files Browse the repository at this point in the history
Add a cancellation callback to OperationObserver
  • Loading branch information
MarkQSchultz committed Nov 25, 2015
2 parents d3cf964 + 3e84556 commit c4d70c2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
8 changes: 7 additions & 1 deletion PSOperations/BlockObserver.swift
Expand Up @@ -16,11 +16,13 @@ public struct BlockObserver: OperationObserver {
// MARK: Properties

private let startHandler: (Operation -> Void)?
private let cancelHandler: (Operation -> Void)?
private let produceHandler: ((Operation, NSOperation) -> Void)?
private let finishHandler: ((Operation, [NSError]) -> Void)?

public init(startHandler: (Operation -> Void)? = nil, produceHandler: ((Operation, NSOperation) -> Void)? = nil, finishHandler: ((Operation, [NSError]) -> Void)? = nil) {
public init(startHandler: (Operation -> Void)? = nil, cancelHandler: (Operation -> Void)? = nil, produceHandler: ((Operation, NSOperation) -> Void)? = nil, finishHandler: ((Operation, [NSError]) -> Void)? = nil) {
self.startHandler = startHandler
self.cancelHandler = cancelHandler
self.produceHandler = produceHandler
self.finishHandler = finishHandler
}
Expand All @@ -31,6 +33,10 @@ public struct BlockObserver: OperationObserver {
startHandler?(operation)
}

public func operationDidCancel(operation: Operation) {
cancelHandler?(operation)
}

public func operation(operation: Operation, didProduceOperation newOperation: NSOperation) {
produceHandler?(operation, newOperation)
}
Expand Down
12 changes: 5 additions & 7 deletions PSOperations/LocationOperation.swift
Expand Up @@ -32,6 +32,11 @@ public class LocationOperation: Operation, CLLocationManagerDelegate {
super.init()
addCondition(LocationCondition(usage: .WhenInUse))
addCondition(MutuallyExclusive<CLLocationManager>())
addObserver(BlockObserver(cancelHandler: { [weak self] _ in
dispatch_async(dispatch_get_main_queue()) {
self?.stopLocationUpdates()
}
}))
}

override public func execute() {
Expand All @@ -56,13 +61,6 @@ public class LocationOperation: Operation, CLLocationManagerDelegate {
}
}

override public func cancel() {
dispatch_async(dispatch_get_main_queue()) {
self.stopLocationUpdates()
super.cancel()
}
}

private func stopLocationUpdates() {
manager?.stopUpdatingLocation()
manager = nil
Expand Down
7 changes: 7 additions & 0 deletions PSOperations/Operation.swift
Expand Up @@ -203,6 +203,13 @@ public class Operation: NSOperation {

didSet {
didChangeValueForKey("cancelledState")
if _cancelled != oldValue && _cancelled == true {

for observer in observers {
observer.operationDidCancel(self)
}

}
}
}

Expand Down
3 changes: 3 additions & 0 deletions PSOperations/OperationObserver.swift
Expand Up @@ -17,6 +17,9 @@ public protocol OperationObserver {
/// Invoked immediately prior to the `Operation`'s `execute()` method.
func operationDidStart(operation: Operation)

/// Invoked immediately after the first time the `Operation`'s `cancel()` method is called
func operationDidCancel(operation: Operation)

/// Invoked when `Operation.produceOperation(_:)` is executed.
func operation(operation: Operation, didProduceOperation newOperation: NSOperation)

Expand Down
4 changes: 4 additions & 0 deletions PSOperations/TimeoutObserver.swift
Expand Up @@ -45,6 +45,10 @@ public struct TimeoutObserver: OperationObserver {
}
}
}

public func operationDidCancel(operation: Operation) {
// No op.
}

public func operation(operation: Operation, didProduceOperation newOperation: NSOperation) {
// No op.
Expand Down
9 changes: 4 additions & 5 deletions PSOperations/URLSessionTaskOperation.swift
Expand Up @@ -31,6 +31,10 @@ public class URLSessionTaskOperation: Operation {
assert(task.state == .Suspended, "Tasks must be suspended.")
self.task = task
super.init()

addObserver(BlockObserver(cancelHandler: { _ in
task.cancel()
}))
}

override public func execute() {
Expand Down Expand Up @@ -59,9 +63,4 @@ public class URLSessionTaskOperation: Operation {
}
}
}

override public func cancel() {
task.cancel()
super.cancel()
}
}
4 changes: 4 additions & 0 deletions PSOperationsTests/PSOperationsTests.swift
Expand Up @@ -51,6 +51,10 @@ class TestObserver: OperationObserver {

}

func operationDidCancel(operation: Operation) {

}

func operationDidFinish(operation: Operation, errors: [NSError]) {
finished = true
self.errors = errors
Expand Down

0 comments on commit c4d70c2

Please sign in to comment.