Executor

public protocol Executor : AnyObject

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 around upon, such as map) are overloaded to take an Executor as well as the standard DispatchQueue.

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:

let context: NSManagedObjectContext = ...
let personJSON: Future<JSON> = ...
let person: Future<Person> = personJSON.map(upon: context) { json in
    Person(json: json, inContext: context)
}
  • submit(_:) Default implementation

    Execute the body closure.

    Default Implementation

    By default, submits the closure contents of the work item.

    Declaration

    Swift

    func submit(_ body: @escaping() -> Void)
  • Execute the workItem.

    Declaration

    Swift

    func submit(_ workItem: DispatchWorkItem)