Advanced-NSOperations/Earthquakes/Operations/NegatedCondition.swift

56 lines
1.8 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:
The file shows how to make an OperationCondition that composes another OperationCondition.
*/
import Foundation
/**
A simple condition that negates the evaluation of another condition.
This is useful (for example) if you want to only execute an operation if the
network is NOT reachable.
*/
struct NegatedCondition<T: OperationCondition>: OperationCondition {
static var name: String {
return "Not<\(T.name)>"
}
static var negatedConditionKey: String {
return "NegatedCondition"
}
static var isMutuallyExclusive: Bool {
return T.isMutuallyExclusive
}
let condition: T
init(condition: T) {
self.condition = condition
}
func dependencyForOperation(operation: EarthquakeOperation) -> Operation? {
return condition.dependencyForOperation(operation: operation)
}
func evaluateForOperation(operation: EarthquakeOperation, completion: @escaping (OperationConditionResult) -> Void) {
condition.evaluateForOperation(operation: operation) { result in
if result == .Satisfied {
// If the composed condition succeeded, then this one failed.
let error = NSError(code: .ConditionFailed, userInfo: [
OperationConditionKey: type(of: self).name,
type(of: self).negatedConditionKey: type(of: self.condition).name
])
completion(.Failed(error))
}
else {
// If the composed condition failed, then this one succeeded.
completion(.Satisfied)
}
}
}
}