SmartDispatchQueue

public class SmartDispatchQueue

A dispatch queue that will call a sync block inline if it can tell we are already on that queue, avoiding the problem of having to know if you are on that queue already before calling sync().

It also supports synchronous execution of the block if on the correct queue already, or flipping that to async if we are not on this queue.

Note

This is not safe to use when using Dispatch Queues that use a target queue. The block will execute on the target queue, and getSpecific will not return the correct value
  • Undocumented

    Declaration

    Swift

    public let queue: DispatchQueue
  • Undocumented

    Declaration

    Swift

    public let queueKey: DispatchSpecificKey<ObjectIdentifier>
  • Initialise with the given queue and an object that is classed as the owner of this smart queue. The owner is used to create a unique key when storing information in the queue.

    Declaration

    Swift

    public init(queue: DispatchQueue, owner: AnyObject)
    • return: true if called on the same queue as this smart queue was initialised

    Declaration

    Swift

    public var isCurrentQueue: Bool { get }
  • Perform a block synchronously, without crashing if called on the same queue as this smart queue

    Declaration

    Swift

    @discardableResult
    public func sync<T>(execute block: () -> T) -> T
  • Perform a block synchronously, without crashing if called on the same queue as this smart queue, or fall back to asynchronous invocation on this queue.

    Declaration

    Swift

    public func syncOrAsyncIfDifferentQueue(execute block: @escaping () -> Void)