TaskProtocol
public protocol TaskProtocol : FutureProtocol where Self.Value : Either
An interface describing a thread-safe way for interacting with the result of some work that may either succeed or fail at some point in the future.
A task
is a superset of a future where the asynchronously-determined value
represents 1 or more exclusive states. These states can be abstracted over
by extensions on the task protocol. The future value is almost always the
TaskResult
type, but many types conforming to TaskProtocol
may exist.
Seealso
FutureProtocol
-
A type that represents the success of some asynchronous work.
Declaration
Swift
associatedtype Success where Self.Success == Self.Value.Right
-
A type that represents the failure of some asynchronous work.
Declaration
Swift
typealias Failure = Error
-
uponSuccess(on:execute:)
Default implementationCall some
body
closure if the task successfully completes.Seealso
FutureProtocol.upon(_:execute:)
Default Implementation
Call some
body
closure if the task successfully completes.Seealso
TaskProtocol.uponSuccess(on:execute:)
See
FutureProtocol.upon(_:execute:)
Declaration
Swift
func uponSuccess(on executor: Executor, execute body: @escaping(_ value: Success) -> Void)
Parameters
executor
A context for handling the
body
.body
A closure to be invoked when the result is determined.
value
The determined success value.
-
uponFailure(on:execute:)
Default implementationCall some
body
closure if the task fails.Seealso
FutureProtocol.upon(_:execute:)
Default Implementation
Call some
body
closure if the task fails.Seealso
TaskProtocol.uponFailure(on:execute:)
Seealso
FutureProtocol.upon(_:execute:)
Declaration
Swift
func uponFailure(on executor: Executor, execute body: @escaping(_ error: Failure) -> Void)
Parameters
executor
A context for handling the
body
.body
A closure to be invoked when the result is determined.
error
The determined failure value.
-
isCancelled
Default implementationTests whether the underlying work has been cancelled.
An implementation should be a
best effort
. By default, no cancellation is supported, and thus cannot become cancelled.Should an implementation choose to be cancellable, it should fully implement cancellability with
cancel
.Default Implementation
Declaration
Swift
var isCancelled: Bool { get }
-
cancel()
Default implementationAttempt to cancel the underlying work.
An implementation should be a
best effort
. By default, no cancellation is supported, and this method does nothing.There are several situations in which a valid implementation may not actually cancel:
- The work has already completed.
- The work has entered an uncancelable state.
- An underlying task is not cancellable.
Should an implementation choose to be cancellable, it should fully implement cancellability with
isCancelled
.Default Implementation
Declaration
Swift
func cancel()
-
andThen(upon:start:)
Extension methodBegins another task by passing the result of the task to
startNextTask
once it completes successfully.On Apple platforms, chaining a task contributes a unit of progress to the root task. A root task is the earliest task in a chain of tasks. If
startNextTask
runs and returns a task that itself reports progress, that progress will also contribute to the chain’s overall progress.Cancelling the resulting task will attempt to cancel both the receiving task and the created task.
Declaration
Swift
public func andThen<NewTask: TaskProtocol>(upon executor: PreferredExecutor, start startNextTask: @escaping(Success) throws -> NewTask) -> Task<NewTask.Success>
-
andThen(upon:start:)
Extension methodBegins another task by passing the result of the task to
startNextTask
once it completes successfully.On Apple platforms, chaining a task contributes a unit of progress to the root task. A root task is the earliest task in a chain of tasks. If
startNextTask
runs and returns a task that itself reports progress, that progress will also contribute to the chain’s overall progress.Cancelling the resulting task will attempt to cancel both the receiving task and the created task.
Note
It is important to keep in mind the thread safety of thestartNextTask
closure.andThen
submitsstartNextTask
toexecutor
once the task completes successfully.See
FutureProtocol.andThen(upon:start:)
-
fallback(upon:to:)
Extension methodBegins another task in the case of the failure of
self
by callingrestartTask
with the error.On Apple platforms, retrying a task contributes a unit of progress to the root task. A root task is the earliest task in a chain of tasks. If
restartTask
runs and returns a task that itself reports progress, that progress will also contribute to the chain’s overall progress.Cancelling the resulting task will attempt to cancel both the receiving task and the created task.
Declaration
Swift
public func fallback<NewTask: TaskProtocol>(upon executor: PreferredExecutor, to restartTask: @escaping(Failure) throws -> NewTask) -> Task<Success> where NewTask.Success == Success
-
fallback(upon:to:)
Extension methodBegins another task in the case of the failure of
self
by callingrestartTask
with the error.On Apple platforms, retrying a task contributes a unit of progress to the root task. A root task is the earliest task in a chain of tasks. If
restartTask
runs and returns a task that itself reports progress, that progress will also contribute to the chain’s overall progress.Cancelling the resulting task will attempt to cancel both the receiving task and the created task.
Note
It is important to keep in mind the thread safety of therestartTask
closure.fallback
submitsrestartTask
toexecutor
once the task fails.See
FutureProtocol.andThen(upon:start:) -
repeat(upon:count:continuingIf:to:)
Extension methodBegin a task immediately by calling
startTask
, then if it fails retry up to thenumberOfAttempts
.Declaration
Swift
public static func `repeat`( upon preferredExecutor: PreferredExecutor, count numberOfAttempts: Int = 3, continuingIf shouldRetry: @escaping(Failure) -> Bool = { _ in return true }, to startTask: @escaping() throws -> Task<Success> ) -> Task<Success>
-
repeat(upon:count:continuingIf:to:)
Extension methodBegin a task immediately by calling
startTask
, then if it fails retry by callingstartTask
up to thenumberOfAttempts
.If
numberOfAttempts
is less than 1,startTask
will still be invoked regardless.If provided,
shouldRetry
will be submitted to theexecutor
to check whether the work can be retried for a given failure. Without a retry predicate, the attempt will always continue.Note
It is important to keep in mind the thread safety of thestartTask
closure. It is initially called in the current execution context, then submitted toexecutor
for any failures.
-
ignored()
Extension methodReturns a task that ignores the successful completion of this task.
This is semantically identical to the following:
myTask.map { _ in }
But behaves more efficiently.
The resulting task is cancellable in the same way the receiving task is.
See
map(transform:)Declaration
Swift
public func ignored() -> Task<Void>
-
map(upon:transform:)
Extension methodReturns a
Task
containing the result of mappingtransform
over the successful task’s value.On Apple platforms, mapping a task reports its progress to the root task. A root task is the earliest task in a chain of tasks. During execution of
transform
, an additional progress object created using the current parent will also contribute to the chain’s progress.The resulting task is cancellable in the same way the receiving task is.
Declaration
Swift
public func map<NewSuccess>(upon queue: PreferredExecutor, transform: @escaping(Success) throws -> NewSuccess) -> Task<NewSuccess>
-
map(upon:transform:)
Extension methodReturns a
Task
containing the result of mappingtransform
over the successful task’s value.The
transform
is submitted to theexecutor
once the task completes.On Apple platforms, mapping a task reports its progress to the root task. A root task is the earliest task in a chain of tasks. During execution of
transform
, an additional progress object created using the current parent will also contribute to the chain’s progress.The resulting task is cancellable in the same way the receiving task is.
See
FutureProtocol.map(upon:transform:)
-
succeed(with:)
Extension methodCompletes the task with a successful
value
, or a thrown error.If
value
represents an expression that may fail by throwing, the task will implicitly catch the failure as the result.Fulfilling this deferred value should usually be attempted only once.
Seealso
PromiseProtocol.fill(with:)
Declaration
Swift
@discardableResult public func succeed(with value: @autoclosure() throws -> Success) -> Bool
-
fail(with:)
Extension methodCompletes the task with a failed
error
.See
fill(with:)Declaration
Swift
@discardableResult public func fail(with error: Failure) -> Bool
-
succeed()
Extension methodCompletes the task with a success.
Fulfilling this deferred value should usually be attempted only once.
Seealso
PromiseProtocol.fill(with:)
Seealso
TaskProtocol.succeed(with:)
Declaration
Swift
@discardableResult public func succeed() -> Bool
-
recover(upon:substituting:)
Extension methodReturns a
Task
containing the result of mappingsubstitution
over the failed task’s error.On Apple platforms, recovering from a failed task reports its progress to the root task. A root task is the earliest task in a chain of tasks. During execution of
transform
, an additional progress object created using the current parent will also contribute to the chain’s progress.The resulting task is cancellable in the same way the receiving task is.
Declaration
Swift
public func recover(upon executor: PreferredExecutor, substituting substitution: @escaping(Failure) throws -> Success) -> Task<Success>
-
recover(upon:substituting:)
Extension methodReturns a
Task
containing the result of mappingsubstitution
over the failed task’s error.recover
submits thesubstitution
to theexecutor
once the task fails.On Apple platforms, recovering from a failed task reports its progress to the root task. A root task is the earliest task in a chain of tasks. During execution of
transform
, an additional progress object created using the current parent will also contribute to the chain’s progress.The resulting task is cancellable in the same way the receiving task is.
See
FutureProtocol.map(upon:transform:)