ThreadLocalValue

public struct ThreadLocalValue<T: Any>

Provides a type-safe mechanism for accessing thread-local values (of type T) stored in the threadDictionary associated with the calling thread.

As the class name implies, values set using ThreadLocalValue are only visible to the thread that set those values.

  • If the receiver was initialized with a namespace, this property will contain that value.

    Declaration

    Swift

    public let namespace: String?
  • The signature of a function to be used for providing values when none exist in the current thread’s threadDictionary.

    Declaration

    Swift

    public typealias ValueProvider = (ThreadLocalValue) -> T?
  • key

    The key that was originally passed to the receiver’s initializer. If the receiver was initialized with a namespace, this value will not include the namespace; fullKey will include the namespace.

    Declaration

    Swift

    public let key: String
  • Contains the key that will be used to access the underlying threadDictionary. Unless the receiver was initialized with a namespace, this value will be the same as key.

    Declaration

    Swift

    public let fullKey: String
  • Initializes a new instance referencing the thread-local value associated with the specified key.

    Declaration

    Swift

    public init(key: String, valueProvider: @escaping ValueProvider = { _ in return nil })

    Parameters

    key

    The key used to access the value associated with the receiver in the threadDictionary.

    valueProvider

    A ValueProvider function used to provide a value when the underlying threadDictionary does not contain a value.

  • Initializes a new instance referencing the thread-local value associated with the specified namespace and key.

    Declaration

    Swift

    public init(namespace: String, key: String, valueProvider: @escaping ValueProvider = { _ in return nil })

    Parameters

    namespace

    The name of the code module that will own the receiver. This is used in constructing the fullKey.

    key

    The key within the namespace. Used to construct the fullKey associated with the receiver.

    valueProvider

    A ValueProvider function used to provide a value when the underlying threadDictionary does not contain a value.

  • Retrieves the current thread’s threadDictionary value associated with the receiver’s fullKey. If the value is nil or if it is not of type T, the receiver’s valueProvider will be consulted to provide a value, which will be stored in the threadDictionary using the key fullKey. nil will be returned if no value was provided.

    Declaration

    Swift

    public var value: T?
  • Retrieves the threadDictionary value currently associated with the receiver’s fullKey. Will be nil if there is no value associated with fullKey or if the underlying value is not of type T.

    Declaration

    Swift

    public var cachedValue: T?
  • Sets a new value in the current thread’s threadDictionary for the key specified by the receiver’s fullKey property.

    Declaration

    Swift

    public func set(_ newValue: T?)

    Parameters

    newValue

    The new thread-local value.