CodableStorage is an easy to use key-value storage for objects conforming to the Codable protocol, backed by Core Data. Both read and save operations are asynchronous and do not block the main thread.
To integrate CodableStorage into your Xcode project you can use Swift Package Manager or CocoaPods. Alternatively you can just manually drag and drop this library to your project.
Saving, reading and removing data looks like in the example below.
structPerson:Codable{letname:Stringletage:Int}
// [...]
Task{@MainActorinletstorage=CodableStorage.default
do{
// save
tryawait storage.setCodable(Person(name:"Peter", age:25), forKey:"person")
// read
letperson=tryawait storage.codable(forKey:"person", type:Person.self)
// remove
tryawait storage.setCodable(nil, forKey:"person")}catch{print("Error: \(error)")}}You can also save arrays with Codable objects in a similar way.
Task{@MainActorinletstorage=CodableStorage.default
do{
// save
letarray=[Person(name:"John", age:35),Person(name:"Kate", age:30)]tryawait storage.setCodable(array, forKey:"people")
// read
letpeople=tryawait storage.codable(forKey:"people", type:[Person].self)
// remove
tryawait storage.setCodable(nil, forKey:"people")}catch{print("Error: \(error)")}}You can also get all currently stored keys using allKeys() and clear the entire storage using clear(). It is possible to create more CodableStorage instances by passing custom URLs to the initializer and then use multiple storages saved in custom locations if needed. You can also use methods with completion handlers if you need to support older system versions and can't use Task.
CodableStorage is available under the MIT license.
