Persister
public final class Persister<Value>
An object that can store and retrieve values from a Storage instance, optionally passing values through a
transformer.
-
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
truethe 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 -
init(cacheValue:valueGetter: valueSetter: valueRemover: defaultValue: defaultValuePersistBehaviour: addUpdateListener: ) Create a new
Persisterinstance.Declaration
Swift
public init( cacheValue: Bool, valueGetter: @escaping ValueGetter, valueSetter: @escaping ValueSetter, valueRemover: @escaping ValueRemover, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [], addUpdateListener: AddUpdateListener )Parameters
cacheValueWhen
truethe persister will cache the latest value in-memory for faster retrieval.valueGetterThe closure that will be called when the
retrieveValue()function is called.valueSetterThe closure that will be called when the
persist(_:)function is called.valueRemoverThe closure that will be called when the
removeValue()function is called.defaultValueThe value to use when a value has not yet been stored, or an error occurs. This value is lazily evaluated.
defaultValuePersistBehaviourAn option set that describes when to persist the default value. Defaults to
[].addUpdateListenerA closure that will be called immediately to add an update listener.
-
Create a new instance that stores the value against the
keyusingstorage, defaulting todefaultValue.Declaration
Swift
public convenience init<Storage: Persist.Storage>( key: Storage.Key, storedBy storage: Storage, cacheValue: Bool = false, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where Storage.Value == ValueParameters
keyThe key to store the value against
storageThe storage to use to persist and retrieve the value.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn option set that describes when to persist the default value. Defaults to
[]. -
Create a new
Persisterinstance that uses the providedStorageto retrieve and store values against the provided key.Declaration
Swift
public convenience init<Storage: Persist.Storage, WrappedValue>( key: Storage.Key, storedBy storage: Storage, cacheValue: Bool = false, defaultValue: @autoclosure @escaping () -> Value = nil, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where Storage.Value == WrappedValue, Value == Optional<WrappedValue>Parameters
keyThe key to retrieve and store values against.
storageThe storage to use to retrieve and store vales.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.
-
Create a new
Persisterinstance that uses the providedStorageto retrieve and store values against the provided key.Declaration
Swift
public convenience init<Storage: Persist.Storage>( key: Storage.Key, storedBy storage: Storage, cacheValue: Bool = false, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where Storage.Value == AnyParameters
keyThe key to retrieve and store values against.
storageThe storage to use to retrieve and store vales.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage. -
Create a new
Persisterinstance that uses the providedStorageto retrieve and store values against the provided key.Declaration
Swift
public convenience init<Storage: Persist.Storage, WrappedValue>( key: Storage.Key, storedBy storage: Storage, cacheValue: Bool = false, defaultValue: @autoclosure @escaping () -> Value = nil, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where Storage.Value == Any, Value == Optional<WrappedValue>Parameters
keyThe key to retrieve and store values against.
storageThe storage to use to retrieve and store vales.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.
-
Create a new
Persisterinstance that uses the providedStorageto retrieve and store values against the provided key. Values will be passed through theTransformerbefore being stored to and 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 Storage.Value == Any, Transformer.Input == ValueParameters
keyThe key to retrieve and store values against.
storageThe storage to use to retrieve and store vales.
transformerThe transformer to use to transform the value when retrieving and storing values.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage. -
Create a new
Persisterinstance that uses the providedStorageto retrieve and store values against the provided key. Values will be passed through theTransformerbefore being stored to and 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 Storage.Value == Any, Transformer.Input == WrappedValue, Value == WrappedValue?Parameters
keyThe key to retrieve and store values against.
storageThe storage to use to retrieve and store vales.
transformerThe transformer to use to transform the value when retrieving and storing values.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.
-
Create a new instance that stores the value against the
keyusingstorage, defaulting todefaultValue. 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.ValueParameters
keyThe key to store the value against
storageThe storage to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn option set that describes when to persist the default value. Defaults to
[]. -
Create a new instance that stores the value against the
keyusingstorage, defaulting todefaultValue. 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
keyThe key to store the value against
storageThe storage to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn option set that describes when to persist the default value. Defaults to
[].
-
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
nilor an error occurs when retrieving the value the default value will be returned.If the
persistWhenNiloption has been provided and the storage returnsnilthe default value will be persisted.If the
persistOnErroroption has been provided and there is an error retrieving the value the default value will be persisted.Declaration
Swift
public func retrieveValue(revalidateCache: Bool) -> ValueParameters
revalidateCacheWhen
truethe cache – if present – will be discarded and the latest value will be retrieved. IfcacheValueistruethis 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
nilthe default value will be returned.If the
persistWhenNiloption has been provided and the storage returnsnilthe default value will be persisted.If the
persistOnErroroption 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 -> ValueReturn 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
nilthe default value will be returned.If the
persistWhenNiloption has been provided and the storage returnsnilthe default value will be persisted.If the
persistOnErroroption 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 -> ValueReturn 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) -> AnyCancellableParameters
updateListenerThe 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 specifiedFileManager, defaulting todefaultValue.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 == DataParameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedFileManager, defaulting todefaultValue.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 == DataParameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedFileManager, defaulting todefaultValue.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 == DataParameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedFileManager, defaulting todefaultValue.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 == DataParameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.Declaration
Swift
public convenience init<WrappedValue>( key: String, storedBy nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore, defaultValue: @autoclosure @escaping () -> Value = nil, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where WrappedValue: StorableInNSUbiquitousKeyValueStore, Value == WrappedValue?Parameters
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.Declaration
Swift
public convenience init<WrappedValue>( key: String, nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore, defaultValue: @autoclosure @escaping () -> Value = nil, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where WrappedValue: StorableInNSUbiquitousKeyValueStore, Value == WrappedValue?Parameters
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.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 nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore, transformer: Transformer, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where Transformer.Input == Value, Transformer.Output: StorableInNSUbiquitousKeyValueStoreParameters
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.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, nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore, transformer: Transformer, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] ) where Transformer.Input == Value, Transformer.Output: StorableInNSUbiquitousKeyValueStoreParameters
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.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
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.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
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.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
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.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
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.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: StorableInUserDefaultsParameters
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.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: StorableInUserDefaultsParameters
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.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
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.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
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
transformerA transformer to transform the value before being persisted and after being retrieved from the storage
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 usingUserDefaultskeys with a dot (.) in them.Declaration
Swift
public static var suppressDotInUserDefaultsKeyWarning: Bool { get set }
-
Create a new instance that stores the value against the
key, storing values using the specifiedFileManager, defaulting todefaultValue.Declaration
Swift
public convenience init( key: URL, storedBy fileManager: FileManager, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedFileManager, defaulting todefaultValue.Declaration
Swift
public convenience init( key: URL, fileManager: FileManager, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedFileManager, defaulting todefaultValue.Declaration
Swift
public convenience init( key: URL, storedBy fileManager: FileManager, defaultValue: @autoclosure @escaping () -> Value = nil, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedFileManager, defaulting todefaultValue.Declaration
Swift
public convenience init( key: URL, fileManager: FileManager, defaultValue: @autoclosure @escaping () -> Value = nil, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
fileManagerThe file manager to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs. Defaults to
nil.defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.Declaration
Swift
public convenience init( key: String, storedBy nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedNSUbiquitousKeyValueStore, defaulting todefaultValue.Declaration
Swift
public convenience init( key: String, nsUbiquitousKeyValueStore: NSUbiquitousKeyValueStore, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
nsUbiquitousKeyValueStoreThe store to use to persist and retrieve the value.
defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.Declaration
Swift
public convenience init( key: String, storedBy userDefaults: UserDefaults, cacheValue: Bool = false, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn 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 specifiedUserDefaults, defaulting todefaultValue.Declaration
Swift
public convenience init( key: String, userDefaults: UserDefaults, cacheValue: Bool = false, defaultValue: @autoclosure @escaping () -> Value, defaultValuePersistBehaviour: DefaultValuePersistOption = [] )Parameters
keyThe key to store the value against
userDefaultsThe user defaults to use to persist and retrieve the value.
cacheValueWhen
truethe latest value will be cached in memory to improve performance when retrieving values, at the cost of increased memory usage.defaultValueThe value to use when a value has not yet been stored, or an error occurs.
defaultValuePersistBehaviourAn option set that describes when to persist the default value. Defaults to
[].
View on GitHub
Persister Class Reference