Group

public class Group

Coroutine groups are useful for canceling multiple coroutines at the same time.

Example:

let group = Coroutine.Group(minimumCapacity: 2)

try group.addCoroutine {
    ...
}

try group.addCoroutine {
    ...
}

// all coroutines in the group will be canceled
group.cancel()
  • Creates a new, empty coroutine group with at least the specified number of elements’ worth of buffer.

    Use this initializer to avoid repeated reallocations of a group’s buffer if you know you’ll be adding elements to the group after creation. The actual capacity of the created group will be the smallest power of 2 that is greater than or equal to minimumCapacity.

    Example:

    let group = CoroutineGroup(minimumCapacity: 2)
    
    try group.addCoroutine {
        ...
    }
    
    try group.addCoroutine {
        ...
    }
    
    // all coroutines in the group will be canceled
    group.cancel()
    

    Declaration

    Swift

    public init(minimumCapacity: Int = 0)

    Parameters

    minimumCapacity

    The minimum number of elements that the newly created group should be able to store without reallocating its buffer.

  • Creates a lightweight coroutine and adds it to the group.

    Example:

    let coroutine = try group.addCoroutine {
        ...
    }
    

    Throws

    The following errors might be thrown:

    VeniceError.canceledCoroutine

    Thrown when the operation is performed within a canceled coroutine.

    VeniceError.outOfMemory

    Thrown when the system doesn’t have enough memory to create a new coroutine.

    Declaration

    Swift

    @discardableResult public func addCoroutine(body: @escaping () throws -> Void) throws -> Coroutine

    Parameters

    body

    Body of the newly created coroutine.

    Return Value

    Newly created coroutine

  • Cancels all coroutines in the group.

    Warning

    Once a coroutine is canceled any coroutine-blocking operation within the coroutine will throw VeniceError.canceledCoroutine.

    Declaration

    Swift

    public func cancel()