GoodPersistence
June 20, 2025 ยท View on GitHub

GoodPersistence
A property wrapper, designed to simplify the process of caching data into the keychain and UserDefaults storage. This wrapper makes it possible to quickly and easily implement caching mechanisms in the applications, without having to write extensive amounts of code. The property wrapper takes care of all the underlying complexity, allowing developers to focus on their application's functionality.
Documentation
You can check GoodPersistence package documentation here
Installation
Swift Package Manager
Create a Package.swift file and add the package dependency into the dependencies list.
Or to integrate without package.swift add it through the Xcode add package interface.
import PackageDescription
let package = Package(
name: "SampleProject",
dependencies: [
.Package(url: "https://github.com/GoodRequest/GoodPersistence" from: "addVersion")
]
)
Usage
Storing to the UserDefaults
@UserDefaultValue(String(describing: AppState.self), defaultValue: .initial)
var appState: AppState
Storing to the KeyChain
@KeychainValue("accessToken", defaultValue: "", accessibility: .afterFirstUnlockThisDeviceOnly)
var accessToken: String
Using Publishers
lazy var appStatePublisher = _appState.publisher
.dropFirst()
.removeDuplicates()
.eraseToAnyPublisher()
Logging
struct SamplePersistenceMonitor: PersistenceMonitor {
func didReceive(_ monitor: any PersistenceMonitor, error: any Error) {
print("Error received: \(error.localizedDescription)")
}
func didReceive(_ monitor: any PersistenceMonitor, message: String) {
print("Message received: \(message)")
}
}
GoodPersistence.Configuration.configure(monitors: [SamplePersistenceMonitor()])
License
GoodPersistence is released under the MIT license. See LICENSE for details.