Advanced-NSOperations/Earthquakes/Operations/NoCancelledDependencies.swift

46 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 an example of implementing the OperationCondition protocol.
*/
import Foundation
/**
A condition that specifies that every dependency must have succeeded.
If any dependency was cancelled, the target operation will be cancelled as
well.
*/
struct NoCancelledDependencies: OperationCondition {
static let name = "NoCancelledDependencies"
static let cancelledDependenciesKey = "CancelledDependencies"
static let isMutuallyExclusive = false
init() {
// No op.
}
func dependencyForOperation(operation: EarthquakeOperation) -> Operation? {
return nil
}
func evaluateForOperation(operation: EarthquakeOperation, completion: (OperationConditionResult) -> Void) {
// Verify that all of the dependencies executed.
let cancelled = operation.dependencies.filter { $0.isCancelled }
if !cancelled.isEmpty {
// At least one dependency was cancelled; the condition was not satisfied.
let error = NSError(code: .ConditionFailed, userInfo: [
OperationConditionKey: type(of: self).name,
type(of: self).cancelledDependenciesKey: cancelled
])
completion(.Failed(error))
}
else {
completion(.Satisfied)
}
}
}