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
bodyclosure if the task successfully completes.Seealso
FutureProtocol.upon(_:execute:)Default Implementation
Call some
bodyclosure 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
executorA context for handling the
body.bodyA closure to be invoked when the result is determined.
valueThe determined success value.
-
uponFailure(on:execute:)Default implementationCall some
bodyclosure if the task fails.Seealso
FutureProtocol.upon(_:execute:)Default Implementation
Call some
bodyclosure 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
executorA context for handling the
body.bodyA closure to be invoked when the result is determined.
errorThe determined failure value.
-
isCancelledDefault 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
startNextTaskonce 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
startNextTaskruns 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
startNextTaskonce 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
startNextTaskruns 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 thestartNextTaskclosure.andThensubmitsstartNextTasktoexecutoronce the task completes successfully.See
FutureProtocol.andThen(upon:start:)
-
fallback(upon:to:)Extension methodBegins another task in the case of the failure of
selfby callingrestartTaskwith 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
restartTaskruns 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
selfby callingrestartTaskwith 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
restartTaskruns 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 therestartTaskclosure.fallbacksubmitsrestartTasktoexecutoronce 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 callingstartTaskup to thenumberOfAttempts.If
numberOfAttemptsis less than 1,startTaskwill still be invoked regardless.If provided,
shouldRetrywill be submitted to theexecutorto 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 thestartTaskclosure. It is initially called in the current execution context, then submitted toexecutorfor 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
Taskcontaining the result of mappingtransformover 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
Taskcontaining the result of mappingtransformover the successful task’s value.The
transformis submitted to theexecutoronce 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
valuerepresents 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
Taskcontaining the result of mappingsubstitutionover 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
Taskcontaining the result of mappingsubstitutionover the failed task’s error.recoversubmits thesubstitutionto theexecutoronce 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:)
View on GitHub
Install in Dash
TaskProtocol Protocol Reference