Controlling Where Callbacks Execute
-
An executor calls closures submitted to it, typically in first-in, first-out order on some other thread. An executor may also model locks or atomicity.
Throughout the Deferred module,
upon
methods (or parameters to methods built aroundupon
, such asmap
) are overloaded to take anExecutor
as well as the standardDispatchQueue
.A custom executor is a customization point into the asynchronous semantics of a future, and may be important for ensuring the thread safety of an
upon
closure.For instance, the concurrency model of Apple’s Core Data framework requires that objects be accessed from other threads using the
perform(_:)
method, and not just thread isolation. Here, we connect that to Deferred:extension NSManagedObjectContext: Executor { func submit(body: @escaping() -> Void) { perform(body) } }
And use it like you would a dispatch queue, with
upon
:
See morelet context: NSManagedObjectContext = ... let personJSON: Future<JSON> = ... let person: Future<Person> = personJSON.map(upon: context) { json in Person(json: json, inContext: context) }
Declaration
Swift
public protocol Executor : AnyObject
-
An operation queue manages a number of operation objects, making high level features like cancellation and dependencies simple.
As an
See moreExecutor
,upon
closures are enqueued as non-cancellable operations. This is ideal for regulating the call relative to other operations in the queue.Declaration
Swift
extension OperationQueue: Executor