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
try 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 try 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.canceled
Thrown when the operation is performed within a canceled coroutine.
VeniceError.outOfMemory
Thrown when the system doesn’t have enough memory to perform the operation.
VeniceError.unexpectedError
Thrown when an unexpected error occurs. This should never happen in the regular flow of an application.
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
cancel
guarantees that all associated resources are deallocated. However, it does not guarantee that the coroutines’ work will have been fully finished. For example, outbound network data may not be flushed.Throws
The following errors might be thrown:
VeniceError.canceled
Thrown when the operation is performed on a canceled coroutine.
VeniceError.unexpectedError
Thrown when an unexpected error occurs. This should never happen in the regular flow of an application.
Declaration
Swift
public func cancel() throws