Task Management

The task manager is a higher level wrapper around GCD that allows you to schedule tasks and also perform interception and reaction operations on the tasks and their results.

See TaskManager

  • A task is any unit of work that is to be carried out. It is protocol based and has a main function called execute that is called when the task is supposed to be executed. When a task is completed, a completion callback is called and passed the result of the task, which is a Result<SuccessValue, Error>.

    Timeouts

    Tasks can implement a timeout that will determine when the TaskManager is to give up on the task.

    Cancellation

    And finally, every task has a Task.didCancel(with:) that is passed a TaskError that tells the Task object why the task was cancelled. The didCancel will only be called after the task has been removed from the TaskManager.

    Notes

    Tasks are reference types because the task manager has to keep track of all the tasks that are floating around.

    See more

    Declaration

    Swift

    public protocol Task : AnyObject
  • This enum represents the various errors that can occur during the execution of a task

    See more

    Declaration

    Swift

    public enum TaskError : Error
  • Declaration

    Swift

    public enum TaskState
  • This is a handle to a task that is given to the TaskManager and can be used to control the task.

    See more

    Declaration

    Swift

    public protocol Handle : AnyObject
  • A task manager can be given an arbitrary number of Tasks and initialized with a set of Interceptors and Reactors, after which it will take care of asynchonous task management for you.

    See more

    Declaration

    Swift

    public class TaskManager
  • A task interceptor allows you to somewhat control what the task manager should do when it encounters a task. The intercept function will be called before the task is executing. This allows you to control the state of the task based on state that you may be interested in.

    Interception commands

    After the intercept function is executed, there’re a number of commands that can be given to the task manager to tell it what to do with the task. The intercept function also contains a current batch count, which tells you how many tasks are being held on to by this interceptor.

    See also

    InterceptCommand
    See more

    Declaration

    Swift

    public protocol Interceptor
  • A command that can be returned by a Interceptor intercept call that tells a TaskManager what it should do with a task before executing it.

    See more

    Declaration

    Swift

    public enum InterceptCommand
  • A task reactor allows you to control what the task manager should do after a task is completed, and before the completion callback is called.

    Multiple reactors

    If a there’re multiple reactors that are supposed to be run for a task then the options set in the ReactorConfiguration are OR'ed together to determine what to do with the task. I.e. if one reactor of n reactors is configured to reqeue a task, then the task will be requeued.

    See more

    Declaration

    Swift

    public protocol Reactor
  • A reactor configuration can be used to control how a reactor is executed, and what happens to tasks while the reactor is being executed.

    Every TaskManager has a queue of tasks and this queue keeps on growing as long as tasks are added to the manager. A reactor has the power to pause this queue while it is being executed and can also tell a manager to requeue any task that causes this reaction

    See more

    Declaration

    Swift

    public struct ReactorConfiguration