PersistenceKit
Store and retrieve Codable objects to various persistence layers, in a couple lines of code!
You love Swift's Codable protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve Codable objects to various persistence layers, in a few lines of code!
Persistence Layers
PersistenceKit offers 3 layers of persistence suitable for most use cases:
1. UserDefaults
- Stores data using
UserDefaults
. - Suitable for storing a reasonable number of objects.
2. Files
- Stores data directly to directories in the app's documents directory using
FileManager
. - Suitable for storing large number of objects.
3. Keychain
- Stores data to OS's keychain using the
Security Framework
. - Suitable for storing sensitive data, like access tokens.
Installation
CocoaPods
To integrate PersistenceKit into your Xcode project using CocoaPods, specify it in your Podfile
:
Carthage
To integrate PersistenceKit into your Xcode project using Carthage, specify it in your Cartfile
:
Swift Package Manager
You can use The Swift Package Manager to install PersistenceKit
by adding the proper description to your Package.swift
file:
Note that the Swift Package Manager is still in early design and development, for more information checkout its GitHub Page
Usage
Let's say you have 2 structs; User
and Laptop
defined as bellow:
1. Conform to the Identifiable
protocol and set the idKey
property
The Identifiable
protocol lets PersistenceKit knows what is the unique id for each object.
Notice how
User
usesInt
for its id, whileLaptop
usesString
, in fact the id can be any type. PersistenceKit uses Swift keypaths to refer to properties without actually invoking them. Swift rocks ?
2 Create Stores
3. Voilà, you're all set!
Requirements
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 10.0+
- Swift 4.2+