Protocols
The following protocols are available globally.
-
Undocumented
See moreDeclaration
Swift
public protocol CoCancellable : AnyObject -
A protocol that defines how to execute a task.
This protocol has extension methods that allow to launch coroutines on a current scheduler. Inside the coroutine you can use such methods as
Coroutine.await(_:),CoFuture.await(), andCoroutineScheduler.await(_:)to suspend the coroutine without blocking a thread and resume it when the result is ready.To launch a coroutine, use
CoroutineScheduler.startCoroutine(_:).//execute coroutine on the main thread DispatchQueue.main.startCoroutine { //extension that returns CoFuture<(data: Data, response: URLResponse)> let dataFuture = URLSession.shared.dataTaskFuture(for: url) //await result that suspends coroutine and doesn't block the thread let data = try dataFuture.await().data }The framework includes the implementation of this protocol for
DispatchQueueand you can easily make the same for other schedulers as well.
See moreextension OperationQueue: CoroutineScheduler { public func scheduleTask(_ task: @escaping () -> Void) { addOperation(task) } }Declaration
Swift
public protocol CoroutineScheduler
Protocols Reference