Persister

public final class Persister<Value>

An object that can store and retrieve values from a Storage instance, optionally passing values through a transformer.

  • An update that was performed by a persister.

    See more

    Declaration

    Swift

    public struct Update
    extension Persister.Update: Hashable where Value: Hashable
    extension Persister.Update: Equatable where Value: Equatable
  • The payload that will be passed to an update listener.

    Declaration

    Swift

    public typealias UpdatePayload = Result<Update, Error>
  • A closure that will be called when an update occurs.

    Declaration

    Swift

    public typealias UpdateListener = (UpdatePayload) -> Void
  • A closure that can retrieve a value.

    Declaration

    Swift

    public typealias ValueGetter = () throws -> Value?
  • A closure that can set a value.

    Declaration

    Swift

    public typealias ValueSetter = (Value) throws -> Void
  • A closure that can remove a value.

    Declaration

    Swift

    public typealias ValueRemover = () throws -> Void
  • A closure that can add an update listener.

    Declaration

    Swift

    public typealias AddUpdateListener = (_ updateListener: @escaping UpdateListener, _ defaultValueGetter: @escaping () -> Value) -> AnyCancellable
  • A publisher that will publish updates as they occur.

    Declaration

    Swift

    @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    public var updatesPublisher: AnyPublisher<UpdatePayload, Never> { get }
  • A publisher that will publish updates as they occur.

    Declaration

    Swift

    @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    public var publisher: AnyPublisher<Value, Never> { get }
  • The default value that will be returned when a value has been be persisted or an error occurs.

    Declaration

    Swift

    public lazy var defaultValue: Value { get set }
  • An option set that describes when to persist the default value.

    Declaration

    Swift

    public var defaultValuePersistBehaviour: DefaultValuePersistOption
  • When true the current value will be cached in memory whenever it is set, retrieved, or the storage provides a new value.

    Declaration

    Swift

    public let cacheValue: Bool
  • Create a new Persister instance.

    Declaration

    Swift

    public init(
        cacheValue: Bool,
        valueGetter: @escaping ValueGetter,
        valueSetter: @escaping ValueSetter,
        valueRemover: @escaping ValueRemover,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = [],
        addUpdateListener: AddUpdateListener
    )

    Parameters

    cacheValue

    When true the persister will cache the latest value in-memory for faster retrieval.

    valueGetter

    The closure that will be called when the retrieveValue() function is called.

    valueSetter

    The closure that will be called when the persist(_:) function is called.

    valueRemover

    The closure that will be called when the removeValue() function is called.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. This value is lazily evaluated.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

    addUpdateListener

    A closure that will be called immediately to add an update listener.

Storage.Value == Value

Storage.Value == Any

Storage.Value == Any, Transformer.Input == Value

Transformer.Input == Value, Transformer.Output == Storage.Value

  • Create a new instance that stores the value against the key using storage, defaulting to defaultValue. Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Storage: Persist.Storage, Transformer: Persist.Transformer>(
        key: Storage.Key,
        storedBy storage: Storage,
        transformer: Transformer,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == Value, Transformer.Output == Storage.Value

    Parameters

    key

    The key to store the value against

    storage

    The storage to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key using storage, defaulting to defaultValue. Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Storage: Persist.Storage, Transformer: Persist.Transformer, WrappedValue>(
        key: Storage.Key,
        storedBy storage: Storage,
        transformer: Transformer,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == WrappedValue, Transformer.Output == Storage.Value, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    storage

    The storage to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Functions

  • Persist the provided value.

    Throws

    Any errors thrown by the storage.

    Declaration

    Swift

    public func persist(_ newValue: Value) throws
  • Declaration

    Swift

    public func retrieveValue() -> Value
  • Attempts to retrieve the value from the storage. If the value is nil or an error occurs when retrieving the value the default value will be returned.

    If the persistWhenNil option has been provided and the storage returns nil the default value will be persisted.

    If the persistOnError option has been provided and there is an error retrieving the value the default value will be persisted.

    Declaration

    Swift

    public func retrieveValue(revalidateCache: Bool) -> Value

    Parameters

    revalidateCache

    When true the cache – if present – will be discarded and the latest value will be retrieved. If cacheValue is true this value will be cached.

    Return Value

    The persisted value, or the default value if no value has been persisted or an error occurs.

  • Attempts to retrieve the value from the storage. If the value is nil the default value will be returned.

    If the persistWhenNil option has been provided and the storage returns nil the default value will be persisted.

    If the persistOnError option has been provided and there is an error retrieving the value the default value will not be persisted and the error will be thrown.

    Throws

    Any error thrown while retrieving the value.

    Declaration

    Swift

    public func retrieveValueOrThrow() throws -> Value

    Return Value

    The persisted value, or the default value if no value has been persisted.

  • Attempts to retrieve the value from the storage. If the value is nil the default value will be returned.

    If the persistWhenNil option has been provided and the storage returns nil the default value will be persisted.

    If the persistOnError option has been provided and there is an error retrieving the value the default value will not be persisted and the error will be thrown.

    Throws

    Any error thrown while retrieving the value.

    Declaration

    Swift

    public func retrieveValueOrThrow(revalidateCache: Bool) throws -> Value

    Return Value

    The persisted value, or the default value if no value has been persisted.

  • Remove the value.

    Throws

    Any errors thrown by the storage.

    Declaration

    Swift

    public func removeValue() throws
  • Add a closure that will be called when the storage notifies the persister of an update.

    Declaration

    Swift

    public func addUpdateListener(_ updateListener: @escaping UpdateListener) -> AnyCancellable

    Parameters

    updateListener

    The closure to call when an update occurs.

    Return Value

    An object that represents the closure’s subscription to changes. This object must be retained by the caller.

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer>(
        key: URL,
        storedBy fileManager: FileManager,
        transformer: Transformer,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == Value, Transformer.Output == Data

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer>(
        key: URL,
        fileManager: FileManager,
        transformer: Transformer,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == Value, Transformer.Output == Data

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer, WrappedValue>(
        key: URL,
        storedBy fileManager: FileManager,
        transformer: Transformer,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Value == WrappedValue?, Transformer.Input == WrappedValue, Transformer.Output == Data

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer, WrappedValue>(
        key: URL,
        fileManager: FileManager,
        transformer: Transformer,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Value == WrappedValue?, Transformer.Input == WrappedValue, Transformer.Output == Data

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Value: StorableInNSUbiquitousKeyValueStore?

Transformer.Input == Value, Transformer.Output: StorableInNSUbiquitousKeyValueStore

Transformer.Input == WrappedValue, Transformer.Output: StorableInNSUbiquitousKeyValueStore

  • Create a new instance that stores the value against the key, storing values in the specified NSUbiquitousKeyValueStore, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer, WrappedValue>(
        key: String,
        storedBy nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore,
        transformer: Transformer,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == WrappedValue, Transformer.Output: StorableInNSUbiquitousKeyValueStore, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    nsUbiquitousKeyValueStore

    The store to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values in the specified NSUbiquitousKeyValueStore, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer, WrappedValue>(
        key: String,
        nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore,
        transformer: Transformer,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == WrappedValue, Transformer.Output: StorableInNSUbiquitousKeyValueStore, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    nsUbiquitousKeyValueStore

    The store to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Value: StorableInUserDefaults?

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init<WrappedValue>(
        key: String,
        storedBy userDefaults: UserDefaults,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where WrappedValue: StorableInUserDefaults, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init<WrappedValue>(
        key: String,
        userDefaults: UserDefaults,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where WrappedValue: StorableInUserDefaults, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Transformer.Input == Value, Transformer.Output: StorableInUserDefaults

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer>(
        key: String,
        storedBy userDefaults: UserDefaults,
        transformer: Transformer,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == Value, Transformer.Output: StorableInUserDefaults

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer>(
        key: String,
        userDefaults: UserDefaults,
        transformer: Transformer,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == Value, Transformer.Output: StorableInUserDefaults

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Transformer.Input == WrappedValue, Transformer.Output: StorableInUserDefaults

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer, WrappedValue>(
        key: String,
        storedBy userDefaults: UserDefaults,
        transformer: Transformer,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == WrappedValue, Transformer.Output: StorableInUserDefaults, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Values stored will be processed by the provided transformer before being persisted and after being retrieved from the storage.

    Declaration

    Swift

    public convenience init<Transformer: Persist.Transformer, WrappedValue>(
        key: String,
        userDefaults: UserDefaults,
        transformer: Transformer,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    ) where Transformer.Input == WrappedValue, Transformer.Output: StorableInUserDefaults, Value == WrappedValue?

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    transformer

    A transformer to transform the value before being persisted and after being retrieved from the storage

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • A property that – when set to true – will suppress the message warning of the downsides of using UserDefaults keys with a dot (.) in them.

    Declaration

    Swift

    public static var suppressDotInUserDefaultsKeyWarning: Bool { get set }

Available where Value == Data

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: URL,
        storedBy fileManager: FileManager,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: URL,
        fileManager: FileManager,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Available where Value == Data?

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: URL,
        storedBy fileManager: FileManager,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values using the specified FileManager, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: URL,
        fileManager: FileManager,
        defaultValue: @autoclosure @escaping () -> Value = nil,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    fileManager

    The file manager to use to persist and retrieve the value.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs. Defaults to nil.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Available where Value: StorableInNSUbiquitousKeyValueStore

  • Create a new instance that stores the value against the key, storing values in the specified NSUbiquitousKeyValueStore, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: String,
        storedBy nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    nsUbiquitousKeyValueStore

    The store to use to persist and retrieve the value.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values in the specified NSUbiquitousKeyValueStore, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: String,
        nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    nsUbiquitousKeyValueStore

    The store to use to persist and retrieve the value.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

Available where Value: StorableInUserDefaults

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: String,
        storedBy userDefaults: UserDefaults,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].

  • Create a new instance that stores the value against the key, storing values in the specified UserDefaults, defaulting to defaultValue.

    Declaration

    Swift

    public convenience init(
        key: String,
        userDefaults: UserDefaults,
        cacheValue: Bool = false,
        defaultValue: @autoclosure @escaping () -> Value,
        defaultValuePersistBehaviour: DefaultValuePersistOption = []
    )

    Parameters

    key

    The key to store the value against

    userDefaults

    The user defaults to use to persist and retrieve the value.

    cacheValue

    When true the latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.

    defaultValue

    The value to use when a value has not yet been stored, or an error occurs.

    defaultValuePersistBehaviour

    An option set that describes when to persist the default value. Defaults to [].