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 aResult<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 aTaskError
that tells theTask
object why the task was cancelled. ThedidCancel
will only be called after the task has been removed from theTaskManager
.Notes
Tasks are reference types because the task manager has to keep track of all the tasks that are floating around.
See moreDeclaration
Swift
public protocol Task : AnyObject
-
This enum represents the various errors that can occur during the execution of a task
See moreDeclaration
Swift
public enum TaskError : Error
-
Declaration
Swift
public enum TaskState
-
This is a handle to a task that is given to the
See moreTaskManager
and can be used to control the task.Declaration
Swift
public protocol Handle : AnyObject
-
A task manager can be given an arbitrary number of
See moreTask
s and initialized with a set ofInterceptor
s andReactor
s, after which it will take care of asynchonous task management for you.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
Declaration
Swift
public protocol Interceptor
-
A command that can be returned by a
See moreInterceptor
intercept call that tells aTaskManager
what it should do with a task before executing it.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
See moreReactorConfiguration
are OR'ed together to determine what to do with the task. I.e. if one reactor ofn
reactors is configured to reqeue a task, then the task will be requeued.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
See moreTaskManager
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 reactionDeclaration
Swift
public struct ReactorConfiguration