ExecutionContext

public protocol ExecutionContext : Retainer

Protocol for concurrency-aware active objects. Conforming to this protocol helps to avoid boilerplate code related to dispatching and memory management. See Moving to nice asynchronous Swift code for complete explanation.

Best way to conform for model-related classes looks like:

public class MyService: ExecutionContext, ReleasePoolOwner {
  private let _internalQueue = DispatchQueue(label: "my-service-queue")
  public var executor: Executor { return .queue(_internalQueue) }
  public let releasePool = ReleasePool()

  /* class implementation */
}

Best way to conform for classes related to main queue looks like:

public class MyMainQueueService: ExecutionContext, ReleasePoolOwner {
  public var executor: Executor { return .main }
  public let releasePool = ReleasePool()

  /* class implementation */
}

Best way to conform for classes related to UI manipulations looks like:

public class MyPresenter: NSObject, ObjCUIInjectedExecutionContext {
  /* class implementation */
}

Classes that conform to NSResponder/UIResponder are automatically conformed to exection context.

  • Executor to perform internal state-changing operations on. It is highly recommended to use serial executor

    Declaration

    Swift

    var executor: Executor { get }
  • Schedules execution of the block

    Declaration

    Swift

    func async(cancellationToken: CancellationToken? = nil,
               block: @escaping (_ strongContext: Self) -> Void)

    Parameters

    cancellationToken

    CancellationToken that can cancel execution

    block

    to schedule after timeout

    strongSelf

    is ExecutionContext restored from weak reference of self

  • Schedules execution of the block after specified timeout

    Declaration

    Swift

    func after(_ timeout: Double, cancellationToken: CancellationToken? = nil,
               block: @escaping (_ strongContext: Self) -> Void)

    Parameters

    timeout

    (in seconds) to execute the block after

    cancellationToken

    CancellationToken that can cancel execution

    block

    to schedule after timeout

    strongSelf

    is ExecutionContext restored from weak reference of self

  • addDependent(cancellable:) Extension method

    Adds dependent Cancellable. Cancellable will be weakly referenced and cancelled on deinit

    Declaration

    Swift

    func addDependent(cancellable: Cancellable)
  • addDependent(completable:) Extension method

    Adds dependent Completable. Completable will be weakly referenced and cancelled because of deallocated context on deinit

    Declaration

    Swift

    func addDependent<T>(completable: T) where T : Completable
  • Makes a dynamic property bound to the execution context

    Declaration

    Swift

    func makeDynamicProperty<T>(
      _ initialValue: T,
      bufferSize: Int = AsyncNinjaConstants.defaultChannelBufferSize
      ) -> DynamicProperty<T>

    Parameters

    initialValue

    initial value to set

    bufferSize

    DerivedChannelBufferSize of derived channel. Keep default value of the argument unless you need an extended buffering options of returned channel

    Return Value

    DynamicProperty bound to the context

  • releaseOnDeinit(_:) Extension method

    Declaration

    Swift

    func releaseOnDeinit(_ object: AnyObject)
  • notifyDeinit(_:) Extension method

    Declaration

    Swift

    func notifyDeinit(_ block: @escaping () -> Void)
  • CustomGetter Extension method

    a getter that could be provided as customization point

    Declaration

    Swift

    typealias CustomGetter<T> = (Self) -> T?
  • CustomSetter Extension method

    a setter that could be provided as customization point

    Declaration

    Swift

    typealias CustomSetter<T> = (Self, T) -> Void
  • makes an UpdatableProperty<T?> for specified key path.

    UpdatableProperty is a kind of Producer so you can:

    • subscribe for updates
    • transform using map, flatMap, filter, debounce, distinct, …
    • update manually with update() method
    • bind Channel to an UpdatableProperty using Channel.bind

    Make sure that keyPath refers to KVO-compliant property.

    • Make sure that properties defined in swift have dynamic attribute.
    • Make sure that methods class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> return correct values for read-only properties

    Declaration

    Swift

    func updatable<T>(
      forKeyPath keyPath: String,
      from originalExecutor: Executor?,
      observationSession: ObservationSession? = nil,
      allowSettingSameValue: Bool = false,
      channelBufferSize: Int = 1,
      customGetter: CustomGetter<T?>? = nil,
      customSetter: CustomSetter<T?>? = nil
      ) -> ProducerProxy<T?, Void>

    Parameters

    keyPath

    to observe.

    originalExecutor

    Executor you calling this method on. Specifying this argument will allow to perform syncronous executions on strictAsync: false Executors. Use default value or nil if you are not sure about an Executor you calling this method on.

    observationSession

    is an object that helps to control observation

    allowSettingSameValue

    set to true if you want to set a new value event if it is equal to an old one

    channelBufferSize

    size of the buffer within returned channel

    customGetter

    provides a custom getter to use instead of value(forKeyPath:) call

    customSetter

    provides a custom getter to use instead of setValue(_: forKeyPath:) call

    Return Value

    an UpdatableProperty<T?> bound to observe and update specified keyPath

  • makes an UpdatableProperty<T> for specified key path.

    UpdatableProperty is a kind of Producer so you can:

    • subscribe for updates
    • transform using map, flatMap, filter, debounce, distinct, …
    • update manually with update() method
    • bind Channel to an UpdatableProperty using Channel.bind

    Make sure that keyPath refers to KVO-compliant property.

    • Make sure that properties defined in swift have dynamic attribute.
    • Make sure that methods class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> return correct values for read-only properties

    Declaration

    Swift

    func updatable<T>(
      forKeyPath keyPath: String,
      onNone: UpdateWithNoneHandlingPolicy<T>,
      from originalExecutor: Executor?,
      observationSession: ObservationSession? = nil,
      allowSettingSameValue: Bool = false,
      channelBufferSize: Int = 1,
      customGetter: CustomGetter<T>? = nil,
      customSetter: CustomSetter<T>? = nil
      ) -> ProducerProxy<T, Void>

    Parameters

    keyPath

    to observe.

    onNone

    is a policy of handling None (or nil) value that can arrive from Key-Value observation.

    executor

    to subscribe and update value on

    originalExecutor

    Executor you calling this method on. Specifying this argument will allow to perform syncronous executions on strictAsync: false Executors. Use default value or nil if you are not sure about an Executor you calling this method on.

    observationSession

    is an object that helps to control observation

    allowSettingSameValue

    set to true if you want to set a new value event if it is equal to an old one

    channelBufferSize

    size of the buffer within returned channel

    customGetter

    provides a custom getter to use instead of value(forKeyPath:) call

    customSetter

    provides a custom getter to use instead of setValue(_: forKeyPath:) call

    Return Value

    an UpdatableProperty<T> bound to observe and update specified keyPath

  • makes an Updating<T?> for specified key path.

    Updating is a kind of Channel so you can:

    • subscribe for updates
    • transform using map, flatMap, filter, debounce, distinct, …

    Make sure that keyPath refers to KVO-compliant property.

    • Make sure that properties defined in swift have dynamic attribute.
    • Make sure that methods class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> return correct values for read-only properties

    Declaration

    Swift

    func updating<T>(
      forKeyPath keyPath: String,
      from originalExecutor: Executor?,
      observationSession: ObservationSession? = nil,
      channelBufferSize: Int = 1,
      customGetter: CustomGetter<T?>? = nil
      ) -> Channel<T?, Void>

    Parameters

    keyPath

    to observe.

    originalExecutor

    Executor you calling this method on. Specifying this argument will allow to perform syncronous executions on strictAsync: false Executors. Use default value or nil if you are not sure about an Executor you calling this method on.

    observationSession

    is an object that helps to control observation

    channelBufferSize

    size of the buffer within returned channel

    customGetter

    provides a custom getter to use instead of value(forKeyPath:) call

    Return Value

    an Updating<T?> bound to observe and update specified keyPath

  • makes an Updating<T> for specified key path.

    Updating is a kind of Channel so you can:

    • subscribe for updates
    • transform using map, flatMap, filter, debounce, distinct, …

    Make sure that keyPath refers to KVO-compliant property.

    • Make sure that properties defined in swift have dynamic attribute.
    • Make sure that methods class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> return correct values for read-only properties

    Declaration

    Swift

    func updating<T>(
      forKeyPath keyPath: String,
      onNone: UpdateWithNoneHandlingPolicy<T>,
      from originalExecutor: Executor?,
      observationSession: ObservationSession? = nil,
      channelBufferSize: Int = 1,
      customGetter: CustomGetter<T>? = nil
      ) -> Channel<T, Void>

    Parameters

    keyPath

    to observe.

    onNone

    is a policy of handling None (or nil) value that can arrive from Key-Value observation.

    originalExecutor

    Executor you calling this method on. Specifying this argument will allow to perform syncronous executions on strictAsync: false Executors. Use default value or nil if you are not sure about an Executor you calling this method on.

    observationSession

    is an object that helps to control observation

    channelBufferSize

    size of the buffer within returned channel

    customGetter

    provides a custom getter to use instead of value(forKeyPath:) call

    Return Value

    an Updating<T> bound to observe and update specified keyPath

  • makes an Updating<(old: T?, new: T?)> for specified key path. With an Updating you can

    • subscribe for updates
    • transform Updating as any Channel (map, flatMap, filter, debounce, distinct, …)

    Make sure that keyPath refers to KVO-compliant property.

    • Make sure that properties defined in swift have dynamic attribute.
    • Make sure that methods class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> return correct values for read-only properties

    Declaration

    Swift

    func updatingOldAndNew<T>(
      forKeyPath keyPath: String,
      from originalExecutor: Executor? = nil,
      observationSession: ObservationSession? = nil,
      channelBufferSize: Int = 1
      ) -> Channel<(old: T?, new: T?), Void>

    Parameters

    keyPath

    to observe.

    originalExecutor

    Executor you calling this method on. Specifying this argument will allow to perform syncronous executions on strictAsync: false Executors. Use default value or nil if you are not sure about an Executor you calling this method on.

    observationSession

    is an object that helps to control observation

    channelBufferSize

    size of the buffer within returned channel

    Return Value

    an Updating<(old: T?, new: T?)> bound to observe and update specified keyPath

  • makes an Updating<[NSKeyValueChangeKey: Any]> for specified key path. With an Updating you can

    • subscribe for updates
    • transform Updating as any Channel (map, flatMap, filter, debounce, distinct, …)

    Make sure that keyPath refers to KVO-compliant property.

    • Make sure that properties defined in swift have dynamic attribute.
    • Make sure that methods class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> return correct values for read-only properties

    Declaration

    Swift

    func updatingChanges(
      forKeyPath keyPath: String,
      from originalExecutor: Executor? = nil,
      options: NSKeyValueObservingOptions,
      observationSession: ObservationSession? = nil,
      channelBufferSize: Int = 1
      ) -> Channel<[NSKeyValueChangeKey: Any], Void>

    Parameters

    keyPath

    to observe.

    originalExecutor

    Executor you calling this method on. Specifying this argument will allow to perform syncronous executions on strictAsync: false Executors. Use default value or nil if you are not sure about an Executor you calling this method on.

    observationSession

    is an object that helps to control observation

    channelBufferSize

    size of the buffer within returned channel

    Return Value

    an Updating<[NSKeyValueChangeKey: Any]> bound to observe and update specified keyPath

  • sink(setter:) Extension method

    Makes a sink that wraps specified setter

    Declaration

    Swift

    func sink<T>(setter: @escaping CustomSetter<T>) -> Sink<T, Void>

    Parameters

    setter

    to use with sink

    Return Value

    constructed sink