RxUserDefaults is a reactive solution for managing user defaults, inspired by rx-preferences with type handling influenced by wrap and unbox.
To create a setting, initialize the class using its constructor:
let settings = RxSettings(userDefaults: userDefaults)
let setting = settings.setting(key: "INSERT_KEY", defaultValue: "DEFAULT")
The arguments are self-explanatory.
Supported Types:
- Array (with types supported by UserDefaults)
- Bool
- Codable (using JSON Decoder)
- Date (as ISO8601 String)
- Double
- Enum (enum must conform to the RxSettingEnum protocol)
- Int
- Set
- String
- UUID
Functions available:
// Retrieve the value
let val = setting.value
// Set the value
setting.value = val
// Check if the value is saved (note: the default value is not automatically saved)
setting.isSet
// Delete the value
setting.remove()
// Provides a hot observable that triggers on every change and starts with the current value (or default value)
setting.asObservable()
If you prefer not to use UserDefaults as a storage layer, you can implement your own by confirming to the StorageLayer Protocol.
The goal is to support all types that UserDefaults supports (e.g., Dictionary, URL). For now, you can expand the library to more types by conforming to the RxSettingCompatible protocol. However, note that persisting types not supported by UserDefaults will fail silently.
RxUserDefaults is available through SPM.
- David Kraus, [email protected]
- Hans-Martin Schuller, [email protected]
- Wolfgang Lutz, [email protected]
Licensed under the Apache License, Version 2.0. See the License for details.