AtomicValue

public final class AtomicValue<Value>

A class that wraps access to its underlying value with an NSLocking instance.

  • Declaration

    Swift

    public init(initialValue: Value)
  • Declaration

    Swift

    public func get() -> Value
  • Declaration

    Swift

    public func set(_ newValue: Value)
  • Sets AtomicValue to newValue and returns the old value

    Declaration

    Swift

    public func getAndSet(_ newValue: Value) -> Value
  • Performs block with the current value, preventing other access until the block exits.

    Declaration

    Swift

    public func atomicallyPerform(block: (Value) -> Void)
  • Performs block with an inout value, preventing other access until the block exits, and enabling the block to mutate the value

    Warning

    The AtomicValue lock is not reentrant. Specifically, it is not possible to call outside the block to get an AtomicValue (e.g., via a convenience property) while inside the with block. Attempting to do so will cause a deadlock.

    Declaration

    Swift

    public func with(block: (inout Value) -> Void)

Available where Value == Bool

  • Toggles the boolean’s value, and returns the old value.

    Example:

    let atomicBool = AtomicValue(initialValue: true)
    print(atomicBool.getAndToggle()) // prints "true"
    print(atomicBool.get()) // prints "false"
    

    Declaration

    Swift

    public func getAndToggle() -> Value

Available where Value: Numeric

  • Increments the current value by amount and returns the incremented value

    Declaration

    Swift

    public func increment(by amount: Value = 1) -> Value
  • Decrements the current value by amount and returns the decremented value

    Declaration

    Swift

    public func decrement(by amount: Value = 1) -> Value

Available where Value: RangeReplaceableCollection

  • Declaration

    Swift

    public func append(_ newElement: Value.Element)
  • Declaration

    Swift

    public func append<S>(contentsOf sequence: S) where S : Sequence, Value.Element == S.Element
  • Declaration

    Swift

    public func removeFirst() -> Value.Element
  • Declaration

    Swift

    public subscript(key: Value.Index) -> Value.Element { get }