Programming Guide

Conditions

Conditions (can) prevent Operations from starting

Conditions are types, usually structs, which conform to OperationCondiiton. They are added to operations just like dependencies and observers.

A silly example would be:

operation.addCondition(BlockCondition { 
    // operation will only be executed if this is true
    return trueOrFalse
})

Evaluating conditions

Before an operation is ready to execute, it will asynchronously evaluate all of its conditions. Each condition will return a result of either .Satisfied or .Failed(ErrorType). If a condition fails, the operation will finish with an error.

But wait, there is more...

Providing a dependency

A condition can also optionally provide an NSOperation as a dependency. This operation will automatically be set as a dependency of the operation the condition was attached to. Therefore, given that all dependencies are executed before the operation evaluates its conditions, it means that whatever crucial detail must be set to satisfy the condition can be performed in another operation. This is pretty neat, and underpins much of the framework.

But wait, there is more...

Mutual exclusion

A condition can assert whether its operation should be evaluated with mutual exclusion of other instances. This can be very handy for stopping operations from executing.

For example, if our application presents a modal alert, by executing this in the context of an operation with a mutually exclusive condition, we will prevent any other modal alert from being presented. Given the nature of event based applications this would otherwise be quite tricky. With Operations this can be done like this:

let alert = AlertOperation(presentAlertFrom: viewController)
alert.title = NSLocaledString("Hello World", comment: "Hello World")
queue.addOperation(alert)

AlertOperation adds a mutually exclusive condition to itself during its initializer.

Provided conditions

The framework comes with a number of operation conditions. Some are simple building blocks, others are more feature rich.

ConditionUsage
AuthorizedForEnsures that the user has allowed permission for the app to access a device or account capability. For example, location services.
BlockConditionEvaluates based on a () -> Bool block.
MutuallyExclusiveIs always satisfied but forces mutual exclusion around a generic type.
NegatedConditionEvaluates the negation of a composed condition.
NoFailedDependenciesConditionAll dependencies must finish without and error or being cancelled.
SilentConditionSuppresses any dependency of a composed condition.
ReachabilityConditionEnsures that the network host is reachable.
RemoteNotificationConditionOn iOS, ensures that the user has been prompted to allow remote notifications.
UserConfirmationConditionOn iOS, will prompt the user to confirm an action via a UIAlertOperation. This is great for asking the user to confirm destructive actions.
UserNotificationConditionOn iOS, ensures that the user has been prompted to allow user notifications.