ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - Teknasyon-Teknoloji/PersistenceKit: Store and retrieve Codable objects to various persistence layers, in a couple lines of code!
Store and retrieve Codable objects to various persistence layers, in a couple lines of code! - Teknasyon-Teknoloji/PersistenceKit
Visit Site

GitHub - Teknasyon-Teknoloji/PersistenceKit: Store and retrieve Codable objects to various persistence layers, in a couple lines of code!

GitHub - Teknasyon-Teknoloji/PersistenceKit: Store and retrieve Codable objects to various persistence layers, in a couple lines of code!

tl;dr

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 default documents directory or shared app group 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.

What's new in v1.3

v1.3 brings Swift 5.0 support

Installation

Usage

Let's say you have 2 structs; User and Laptop defined as bellow:

struct User: Codable {
	var id: Int
	var firstName: String
	var lastName: String
	var laptop: Laptop?
}
struct Laptop: Codable {
	var model: String
	var name: String
}

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.

struct User: Codable, Identifiable {
	static let idKey = \User.id
	...
}
struct Laptop: Codable, Identifiable {
	static let idKey = \Laptop.model
	...
}

Notice how User uses Int for its id, while Laptop uses String, 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

// To save objects to UserDefaults, create UserDefaultsStore:
let usersStore = UserDefaultsStore<User>(uniqueIdentifier: "users")!
let laptopsStore = UserDefaultsStore<Laptop>(uniqueIdentifier: "laptops")!

// To save a single object to UserDefaults, create UserDefaultsStore:
let userStore = SingleUserDefaultsStore<User>(uniqueIdentifier: "user")!

// If you want to share data between app and extentions:
let sharedUsersStore = UserDefaultsStore<User>(uniqueIdentifier: "users", groupIdentifier: "com.yourCompany.app")!
let sharedUserStore = SingleUserDefaultsStore<User>(uniqueIdentifier: "user", groupIdentifier: "com.yourCompany.app")!

// To save objects to the file system, create FilesStore:
let usersStore = FilesStore<User>(uniqueIdentifier: "users")
let laptopsStore = FilesStore<Laptop>(uniqueIdentifier: "laptops")

// To save objects to the app group shared file system, create FilesStore:
let appGroup = Bundle.main.infoDictionary?["appGroup"] as? String ?? "group.company.app"
let usersStore = FilesStore<User>(uniqueIdentifier: "users", groupIdentifier: appGroup)
let laptopsStore = FilesStore<Laptop>(uniqueIdentifier: "laptops", groupIdentifier: appGroup)

// To save a single object to the file system, create SingleFilesStore:
let userStore = SingleFilesStore<User>(uniqueIdentifier: "user")

// To save a single object to the app group shared file system, create SingleFilesStore:
let appGroup = Bundle.main.infoDictionary?["appGroup"] as? String ?? "group.company.app"
let userStore = SingleFilesStore<User>(uniqueIdentifier: "user", groupIdentifier: appGroup)

// To save a single object to the system's keychain, create SingleKeychainStore:
let userStore = SingleKeychainStore<User>(uniqueIdentifier: "user")

3. Voilà, you're all set!

let macbook = Laptop(model: "A1278", name: "MacBook Pro")
let john = User(userId: 1, firstName: "John", lastName: "Appleseed", laptop: macbook)

// Save an object to a store
try! usersStore.save(john)

// Save an array of objects to a store
try! usersStore.save([jane, steve, jessica])

// Get an object from store
let user = store.object(withId: 1)
let laptop = store.object(withId: "A1278")

// Get all objects in a store
let laptops = laptopsStore.allObjects()

// Check if store has an object
print(usersStore.hasObject(withId: 10)) // false

// Iterate over all objects in a store
laptopsStore.forEach { laptop in
	print(laptop.name)
}

// Delete an object from a store
usersStore.delete(withId: 1)

// Delete all objects in a store
laptops.deleteAll()

// Know how many objects are stored in a store
let usersCount = usersStore.objectsCount

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 10.0+
  • Swift 4.2+

Thanks

Special thanks to:

Credits

License

PersistenceKit is released under the MIT license. See LICENSE for more information.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory