AnyTask

  • An AnyTask can be used as a generic Task object. You have to pass it a execute block when it’s initialized and that will be called when it’s time to execute the task.

    AnyTasks schedules execution blocks on TaskManager.shared.

    See more

    Declaration

    Swift

    public class AnyTask<T> : Task
  • Creates a task out of an expression and returns it as an AnyTask

    let x = task(someLongOperation())
    
    x.async { result in
       // stuff
    }
    
    // or
    
    let result = x.await()
    

    Declaration

    Swift

    public func task<R>(closingOver closure: @autoclosure @escaping () -> R) -> AnyTask<R>

    Parameters

    closingOver

    the expression to create a task out of

  • Creates a task out of a function that has a done callback. I.e.

    let f = { (done: @escaping () -> Void) -> Void in
       DispatchQueue.global(qos: .unspecified).async {
           // Do some long running calculation
           done()
       }
    }
    let t = task(executing: f)
    

    Declaration

    Swift

    public func task(executing block: @escaping (@escaping () -> Void) -> Void) -> AnyTask<Void>

    Parameters

    executing

    an asynchronous function that has a done callback as it’s only parameter. The done callback must be called when the asynchronous operation completes.

  • Creates a task out of a function that has a done callback with a specific signature

    Declaration

    Swift

    public func task<T>(executing block: @escaping (@escaping (T) -> Void) -> Void) -> AnyTask<T>

    Parameters

    executing

    an asynchronous function that has a done callback as it’s only parameter. The done callback must take the expected value of the asynchronous operation as its only parameter

  • Creates a task out of a function that has a done callback with a specific signature

    Declaration

    Swift

    public func task<T>(executing block: @escaping (@escaping (Result<T, Error>) -> Void) -> Void) -> AnyTask<T>

    Parameters

    executing

    an asynchronous function that has a done callback as it’s only parameter. The done callback must take the expected Result<T, Error> of the asynchronous operation as its only parameter