Store your awsome Codable
objects and retrieve them with ease. CodablePersist
gives you a convenient way to store your objects in a disk, UserDefaluts, and memory and manage them easily.
- ✅ Easily save and retrieve any
Codable
type. - 📆 Set expiration date to the objects.
- 🔍Time-based storage filtring.
- 🗃Retrieve, delete, save objects using subscript
To quickly show you how CodablePersist
can be useful, consider the following use case:
class PostFetcher {
typealias Handler = (Result<Post, Error>) -> Void
private let cache = DiskStorage<Post>(storeName: "postStorage")
func fetchPost(withID id: Post.ID, then handler: @escaping Handler) {
// check if the post is cached or not
if let cached = cache[id] { return handler(.success(cached)) }
// if not cached fetch it from the backend
performFetching { [weak self] result in
// load post and cache it
let post = try? result.get() post.map { self?.cache[id] = $0 }
//then return the result
handler(result)
}
}
}
CodablePersist is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'CodablePersist'
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
To integrate CodablePersist into your Xcode project using Carthage, specify it in your Cartfile
:
github "engali94/CodablePersist"
Run carthage update
to build the framework and drag the built CodablePersist.framework
into your Xcode project.
On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase” and add the Framework path as mentioned in Carthage Getting started Step 4, 5 and 6
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/engali94/CodablePersist.git", from: "0.1")
]
Next, add CodablePersist
to your targets as follows:
.target(
name: "YOUR_TARGET_NAME",
dependencies: [
"CodablePersist",
]
),
Then run swift package update
to install the package.
Alternatively navigate to your Xcode project, select Swift Packages
and click the +
icon to search for CodablePersist
.
If you prefer not to use any of the aforementioned dependency managers, you can integrate CodablePersist into your project manually. Simply drag the Sources
Folder into your Xcode project.
Make sure your type conforms to Identifiable
protocol, and assign a unique idKey
property like follows:
struct Post: Codable, Identifiable {
// Identifiable conformance
static var idKey = \Post.id
var title: String
var id: String // should be unique per post
}
-
DiskStorage
: We can init a Disk Storage by passing astoreName
andexpiryDate
let storage = try? DiskStorage<Post>(storeName: storageName, expiryDate: .minutes(interval: 10))
-
UserDefalutsStorage
: Also we can init UserDefaults Storage by passing astoreName
andexpiryDate
let storage = UserDefaultsStorage<Post>(storeName: storageName, expiryDate: .minutes(interval: 10))!
-
MemoryStorage
: This should be used with precaution, if any memory load happens the system will delete some or all objects to free memory. Consider usingDiskStorage
orUserDefalutsStorage
for long term persistence. we can init ``let storage = MemoryStorage<Post>(expiryDate: .minutes(interval: 10))
Now you are good to go... You can persist, retrieve and delete your
Codable
objects 😎let singlePost = Post(title: "I'm persistable", id: 1) let posts: [Post] = [Post(title: "I'm persistable2", id: 2), Post(title: "I'm persistable3", id: 3)] //Save a single object try? storage.save(singlePost) // Save single object by subscript storage[1] = singlePost //Save mutliple objects try? storage.save(posts)
// fetch single object let post1 = try? storage.fetchObject(for: 1) // fetch by subscript let post2 = storage[2] // fetch multiple objects let multiPosts = try? storage.fetchObjects(for: [3,2]) // fetch all objects in the store sorted by date ascendingly let allObjets = try? storage.fetchAllObjects(descending: false) // fetch only objects saved in las ten minutes // ------(10)++++++(now) -> will only returns ++++++ let obejetsAfterTenMin = try? storage.fetchObjectsStored(inLast: .minutes(interval: 10) // fetch object stored before the last 10 minutes // ++++++(10)------(now) -> will only returns ++++++ let obejetsBeforeTemMin = try? storage.fetchObjectsStored(before: .minutes(interval: 10)) // check if an object exists in the storage storage.contains(2) // check the number of objects in the storage storage.objectsCount
// delete a single object try? storage.deleteObject(forKey: singlePost.id) // delete a single object by subscript storage[singlePost.id] = nil // delete multiple posts try? storage.deleteObjects(forKeys: [2,3]) // delete all objects try? storage.deleteAll() // Delete expired objects storage.deleteExpired()
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 10.0+
- Swift 4.2+
Contributions are warmly welcomed 🙌
CodablePesist is released under the MIT license. See LICENSE for more information.