Delay Operation

The DelayOperation inserts a delay into the queue. It does not perform any work itself. It can be created using either a time interval, e.g. delay for 2 seconds, or with a NSDate instance which must be in the future.

let delayInterval = DelayOperation(interval: 1)
let delayDate = DelayOperation(date: dateInTheFuture)
queue.addOperations(delayInterval, delayDate)

Note that the above code is pretty meaningless. DelayOperation will only pause a serial queue, and delaying by one second, and then until a date in the future is likely redundant.

Instead, DelayOperation is useful for delaying the executing of other operations by adding it as a dependency.

let delay = DelayOperation(interval: 2)
let operation = BlockOperation { print("Hello World!") }
operation.addDependency(delay)
queue.addOperations(delay, operation)

This can be very useful if implementing an exponential back-off system, or other repeating task.


Did this page help you?