BlockTimer

public final class BlockTimer : ScopeFunctions

Block based timer that uses Grand Central Dispatch internally.

final class MyClass {
   private let timer: BlockTimer

   init() {
       self.timer = .scheduled(timeInterval: 1.0, repeats: true) { _ in
           // do something every 1 second
       }
   }
}

Public Types

  • Typealias for block that is invoked whenever the timer fires.

    Declaration

    Swift

    public typealias Block = (BlockTimer) -> Void

Public Properties

  • Get/set the default repeats value.

    Declaration

    Swift

    public static var defaultRepeats: Bool
  • Get/set the default target queue where the timer will fire.

    Declaration

    Swift

    public static var defaultTargetQueue: DispatchQueue

Public Functions

  • Returns a timer after calling BlockTimer.schedule() on it.

    Declaration

    Swift

    public static func scheduled(timeInterval: TimeInterval, repeats: Bool = BlockTimer.defaultRepeats, targetQueue: DispatchQueue = BlockTimer.defaultTargetQueue, block: @escaping Block) -> BlockTimer

    Parameters

    timeInterval

    The time interval at which the timer should fire

    repeats

    Whether the timer should repeat

    targetQueue

    The queue on which the timer should fire

    block

    The block to invoke when the timer fires

    Return Value

    The scheduled timer

  • Schedules the timer to begin firing.

    Important

    Calling this multiple times has no effect

    Declaration

    Swift

    public func schedule()
  • Invalidates a previously scheduled timer.

    Important

    Calling this multiple times has no effect

    Declaration

    Swift

    public func invalidate()
  • Invokes the block that was provided during initialization asynchronously on provided target queue.

    Important

    Calling this on a timer that has been invalidated does nothing

    Declaration

    Swift

    public func fire()

Initializers

  • Creates an instance with the provided parameters.

    Declaration

    Swift

    public init(timeInterval: TimeInterval, repeats: Bool = BlockTimer.defaultRepeats, targetQueue: DispatchQueue = BlockTimer.defaultTargetQueue, block: @escaping Block)

    Parameters

    timeInterval

    The time interval at which the timer should fire

    repeats

    Whether the timer should repeat

    targetQueue

    The queue on which the timer should fire

    block

    The block to invoke when the timer fires

    Return Value

    The instance