Lifetime

public final class Lifetime

Represents the lifetime of an object, and provides a hook to observe when the object deinitializes.

  • A signal that sends a completed event when the lifetime ends.

    Note

    Consider using Lifetime.observeEnded if only a closure observer is to be attached.

    Declaration

    Swift

    public var ended: Signal<Never, Never> { get }
  • A flag indicating whether the lifetime has ended.

    Declaration

    Swift

    public var hasEnded: Bool { get }
  • Initialize a Lifetime from a lifetime token, which is expected to be associated with an object.

    Important

    The resulting lifetime object does not retain the lifetime token.

    Declaration

    Swift

    public convenience init(_ token: Token)

    Parameters

    token

    A lifetime token for detecting the deinitialization of the associated object.

  • Observe the termination of self.

    Declaration

    Swift

    @discardableResult
    public func observeEnded(_ action: @escaping () -> Void) -> Disposable?

    Parameters

    action

    The action to be invoked when self ends.

    Return Value

    A disposable that detaches action from the lifetime, or nil if lifetime has already ended.

  • Add the given disposable as an observer of self.

    Declaration

    Swift

    @discardableResult
    public static func += (lifetime: Lifetime, disposable: Disposable?) -> Disposable?

    Parameters

    disposable

    The disposable to be disposed of when self ends.

    Return Value

    A disposable that detaches disposable from the lifetime, or nil if lifetime has already ended.

  • Factory method for creating a Lifetime and its associated Token.

    Declaration

    Swift

    public static func make() -> (lifetime: Lifetime, token: Token)

    Return Value

    A (lifetime, token) tuple.

  • A Lifetime that has already ended.

    Declaration

    Swift

    public static let empty: Lifetime
  • A token object which completes its associated Lifetime when it deinitializes, or when dispose() is called.

    It is generally used in conjuncion with Lifetime as a private deinitialization trigger.

    class MyController {
        private let (lifetime, token) = Lifetime.make()
    }
    

    Declaration

    Swift

    public final class Token