• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Store and retrieve Codable objects to various persistence layers, in a couple lines of code!

Build Status Test Coverage Platforms Cocoapods Carthage compatible Swift Package Manager compatible Swift Xcode MIT

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

CocoaPods

To integrate PersistenceKit into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'PersistenceKit'
Carthage

To integrate PersistenceKit into your Xcode project using Carthage, specify it in your Cartfile:

github "Teknasyon-Teknoloji/PersistenceKit"
Swift Package Manager

You can use The Swift Package Manager to install PersistenceKit by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
  name: "YOUR_PROJECT_NAME",
  targets: [],
  dependencies: [
    .package(url: "https://github.com/Teknasyon-Teknoloji/PersistenceKit.git", from: "0.1")
  ]
)

Note that the Swift Package Manager is still in early design and development, for more information checkout its GitHub Page

Manually

Add the Sources folder to your Xcode project.

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 Repositories

1

pyfbad

An open-source unsupervised time-series anomaly detection package by Getcontact Data Team
Jupyter Notebook
43
star
2

desk360-livechat-android-sdk

Desk360 Mobile Chat SDK for Android
Kotlin
30
star
3

Readyalog

A dependency-free android dialog library. Uses regular support-v7 only.
Java
15
star
4

desk360-android-sdk

Desk360 Android SDK
Kotlin
14
star
5

desk360-flutter-sdk

This package gives wrapper methods for Desk360 SDK.
Swift
11
star
6

desk360-react-native-sdk

This package gives wrapper methods for Desk360 SDK.
JavaScript
11
star
7

deepwall-ios-sdk

Managing apps that offer in-app purchases have never been easier with Deepwall
Objective-C
9
star
8

aws-ses-notification

Handle AWS Ses notifications e.g. bounced, complaint or delivery.
PHP
8
star
9

desk360-ios-sdk

Desk360 iOS SDK
Swift
8
star
10

EasyPay

Simple android in app billing project. By adding this module in your project, you can earn money quickly and easly!
Java
7
star
11

phalconphp-auth

Authentication package for PhalconPHP framework.
PHP
7
star
12

docker

Docker for PHP Projects
Nginx
7
star
13

deepwall-flutter-sdk

This package gives wrapper methods for deepwall sdks.
Swift
6
star
14

Getcontact-Bootcamp

Getcontact Bootcamp Android
Kotlin
6
star
15

git-commits-style

Teknasyon Git Commits Style Guidelines
6
star
16

user-intent

A tiny library to detect user intent (exit intent) and trigger a callback. It's built with pure JavaScript and supports multiple triggers. Made for better user engagements.
JavaScript
6
star
17

isbank-php

İşbank web servisleri için PHP kütüphanesi
PHP
5
star
18

desk360-livechat-ios-sdk

Desk360 Mobile Chat SDK for iOS
Swift
5
star
19

guzzle-async-pool

Bu kütüphane GuzzleHttp\Pool sınıfının özellikle Soap için kullanımını kolaylaştırmayı amaçlamaktadır.
PHP
4
star
20

deepwall-react-native-sdk

This package gives wrapper methods for deepwall sdks.
JavaScript
3
star
21

deepwall-android-sdk

Managing apps that offer in-app purchases have never been easier with Deepwall Android SDK
3
star
22

slack-bonus-cmd

PHP
3
star
23

composer-update-zd

Composer Zero Downtime Update Shell Script
Shell
2
star
24

crond

Distributed Cron Daemon with PHP
PHP
2
star
25

MoyaTodos

Simple todos app using Moya for demo purposes
Swift
1
star
26

thy-sdk-php

Official repository of the THY SDK for PHP (@thyforphp)
1
star
27

DecoratorPatternPHP

A simple decorator pattern explanation using MySQL and Redis on PHP.
PHP
1
star
28

deepwall-unity-sdk

This plugin gives wrapper methods for deepwall sdks.
C#
1
star
29

boun-menu-ios

BOUN Menu iOS App
Swift
1
star
30

hera-ios-sdk

Hera is a mediation management library for iOS
Swift
1
star