Classes
The following classes are available globally.
-
Channel is a non-blocking primitive for communication between a sender and a receiver. Conceptually, a channel is similar to a queue that allows to suspend a coroutine on receive if it is empty or on send if it is full.
Important
Alwaysclose()
orcancel()
a channel when you are done to resume all suspended coroutines by the channel.
See morelet channel = CoChannel<Int>(maxBufferSize: 1) DispatchQueue.global().startCoroutine { for i in 0..<10 { try channel.awaitSend(i) } channel.close() } DispatchQueue.global().startCoroutine { for i in channel { print("Receive", i) } print("Done") }
Declaration
Swift
public final class CoChannel<Element>
extension CoChannel: Sequence
-
Holder for a result that will be provided later.
CoFuture
and its subclassCoPromise
are the implementation of the Future/Promise approach. They allow to launch asynchronous tasks and immediately returnCoFuture
with its future results. The available result can be observed by thewhenComplete()
callback or byawait()
inside a coroutine without blocking a thread.
See morefunc makeFutureOne(args) -> CoFuture<Response> { let promise = CoPromise<Response>() someAsyncFuncWithCallback { response in . . . do some work . . . promise.success(response) } return promise } func makeFutureTwo(args) -> CoFuture<Response> { queue.coroutineFuture { let future = makeFutureOne(args) . . . do some work . . . let response = try future.await() . . . create result using response . . . return result } } func performSomeWork(args) { let future = makeFutureTwo(args) mainQueue.startCoroutine { . . . do some work . . . let result = try future.await() . . . do some work using result . . . } }
Declaration
Swift
public class CoFuture<Value>
extension CoFuture: Hashable
extension CoFuture: Cancellable