{"id":1011,"url":"https://github.com/Teknasyon-Teknoloji/PersistenceKit","last_synced_at":"2025-08-06T13:32:10.777Z","repository":{"id":33277907,"uuid":"153740868","full_name":"Teknasyon-Teknoloji/PersistenceKit","owner":"Teknasyon-Teknoloji","description":"Store and retrieve Codable objects to various persistence layers, in a couple lines of code!","archived":false,"fork":false,"pushed_at":"2024-11-11T11:48:53.000Z","size":854,"stargazers_count":155,"open_issues_count":1,"forks_count":14,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-29T20:54:55.720Z","etag":null,"topics":["filemanager","ios","keychain","macos","swift","tvos","userdefaults","watchos"],"latest_commit_sha":null,"homepage":"https://teknasyon-teknoloji.github.io/PersistenceKit/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Teknasyon-Teknoloji.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-19T07:07:58.000Z","updated_at":"2024-11-11T11:48:16.000Z","dependencies_parsed_at":"2022-08-07T20:17:36.602Z","dependency_job_id":null,"html_url":"https://github.com/Teknasyon-Teknoloji/PersistenceKit","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Teknasyon-Teknoloji%2FPersistenceKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Teknasyon-Teknoloji%2FPersistenceKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Teknasyon-Teknoloji%2FPersistenceKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Teknasyon-Teknoloji%2FPersistenceKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Teknasyon-Teknoloji","download_url":"https://codeload.github.com/Teknasyon-Teknoloji/PersistenceKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228905450,"owners_count":17989770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filemanager","ios","keychain","macos","swift","tvos","userdefaults","watchos"],"created_at":"2024-01-05T20:15:36.995Z","updated_at":"2024-12-09T14:30:46.510Z","avatar_url":"https://github.com/Teknasyon-Teknoloji.png","language":"Swift","funding_links":[],"categories":["Database","Libs","Data and Storage","Data Management [🔝](#readme)"],"sub_categories":["Getting Started","Data Management","Linter"],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/Teknasyon-Teknoloji/PersistenceKit/master/Assets/logo.png\" title=\"PersistenceKit\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://travis-ci.org/Teknasyon-Teknoloji/PersistenceKit\"\u003e\u003cimg src=\"https://travis-ci.org/Teknasyon-Teknoloji/PersistenceKit.svg?branch=master\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/Teknasyon-Teknoloji/PersistenceKit\"\u003e\u003cimg src=\"https://codecov.io/gh/Teknasyon-Teknoloji/PersistenceKit/branch/master/graph/badge.svg\" alt=\"Test Coverage\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/Teknasyon-Teknoloji/PersistenceKit\"\u003e\u003cimg src=\"https://img.shields.io/cocoapods/p/PersistenceKit.svg?style=flat\" alt=\"Platforms\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://cocoapods.org/pods/PersistenceKit\"\u003e\u003cimg src=\"https://img.shields.io/cocoapods/v/PersistenceKit.svg\" alt=\"Cocoapods\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/Carthage/Carthage\"\u003e\u003cimg src=\"https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat\" alt=\"Carthage compatible\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://swift.org/package-manager/\"\u003e\u003cimg src=\"https://img.shields.io/badge/SPM-compatible-4BC51D.svg?style=flat\" alt=\"Swift Package Manager compatible\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://swift.org\"\u003e\u003cimg src=\"https://img.shields.io/badge/Swift-4.2-orange.svg\" alt=\"Swift\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://developer.apple.com/xcode\"\u003e\u003cimg src=\"https://img.shields.io/badge/Xcode-10-blue.svg\" alt=\"Xcode\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/Teknasyon-Teknoloji/PersistenceKit/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/License-MIT-red.svg\" alt=\"MIT\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\n## tl;dr\nYou love Swift's Codable protocol and use it everywhere, who doesn't!\nHere is an easy and very light way to store and retrieve `Codable` objects to various persistence layers, in a few lines of code!\n\n## Persistence Layers\n\nPersistenceKit offers 3 layers of persistence suitable for most use cases:\n\n### 1. UserDefaults\n- Stores data using [`UserDefaults`](https://developer.apple.com/documentation/foundation/userdefaults).\n- Suitable for storing a reasonable number of objects.\n\n### 2. Files\n- Stores data directly to directories in the app's default documents directory or shared app group directory using [`FileManager`](https://developer.apple.com/documentation/foundation/filemanager).\n- Suitable for storing large number of objects.\n\n### 3. Keychain\n- Stores data to OS's keychain using the [`Security Framework`](https://developer.apple.com/documentation/security).\n- Suitable for storing sensitive data, like access tokens.\n\n## What's new in v1.3\n\nv1.3 brings Swift 5.0 support\n\n## Installation\n\n\u003cdetails\u003e\n\u003csummary\u003eCocoaPods\u003c/summary\u003e\n\u003c/br\u003e\n\u003cp\u003eTo integrate PersistenceKit into your Xcode project using \u003ca href=\"http://cocoapods.org\"\u003eCocoaPods\u003c/a\u003e, specify it in your \u003ccode\u003ePodfile\u003c/code\u003e:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"ruby language-ruby\"\u003epod 'PersistenceKit'\u003c/code\u003e\u003c/pre\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eCarthage\u003c/summary\u003e\n\u003c/br\u003e\n\u003cp\u003eTo integrate PersistenceKit into your Xcode project using \u003ca href=\"https://github.com/Carthage/Carthage\"\u003eCarthage\u003c/a\u003e, specify it in your \u003ccode\u003eCartfile\u003c/code\u003e:\u003c/p\u003e\n\n\u003cpre\u003e\u003ccode class=\"ogdl language-ogdl\"\u003egithub \"Teknasyon-Teknoloji/PersistenceKit\"\n\u003c/code\u003e\u003c/pre\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eSwift Package Manager\u003c/summary\u003e\n\u003c/br\u003e\n\u003cp\u003eYou can use \u003ca href=\"https://swift.org/package-manager\"\u003eThe Swift Package Manager\u003c/a\u003e to install \u003ccode\u003ePersistenceKit\u003c/code\u003e by adding the proper description to your \u003ccode\u003ePackage.swift\u003c/code\u003e file:\u003c/p\u003e\n\n\u003cpre\u003e\u003ccode class=\"swift language-swift\"\u003eimport PackageDescription\n\nlet package = Package(\n  name: \"YOUR_PROJECT_NAME\",\n  targets: [],\n  dependencies: [\n    .package(url: \"https://github.com/Teknasyon-Teknoloji/PersistenceKit.git\", from: \"0.1\")\n  ]\n)\n\u003c/code\u003e\u003c/pre\u003e\n\n\u003cp\u003eNote that the \u003ca href=\"https://swift.org/package-manager\"\u003eSwift Package Manager\u003c/a\u003e is still in early design and development, for more information checkout its \u003ca href=\"https://github.com/apple/swift-package-manager\"\u003eGitHub Page\u003c/a\u003e\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eManually\u003c/summary\u003e\n\u003c/br\u003e\n\u003cp\u003eAdd the \u003ca href=\"https://github.com/Teknasyon-Teknoloji/PersistenceKit/tree/master/Sources\"\u003eSources\u003c/a\u003e folder to your Xcode project.\u003c/p\u003e\n\u003c/details\u003e\n\n\n## Usage\n\nLet's say you have 2 structs; `User` and `Laptop` defined as bellow:\n\n```swift\nstruct User: Codable {\n\tvar id: Int\n\tvar firstName: String\n\tvar lastName: String\n\tvar laptop: Laptop?\n}\n```\n\n```swift\nstruct Laptop: Codable {\n\tvar model: String\n\tvar name: String\n}\n```\n\n### 1. Conform to the `Identifiable` protocol and set the `idKey` property\n\nThe `Identifiable` protocol lets PersistenceKit knows what is the unique id for each object.\n\n```swift\nstruct User: Codable, Identifiable {\n\tstatic let idKey = \\User.id\n\t...\n}\n```\n\n```swift\nstruct Laptop: Codable, Identifiable {\n\tstatic let idKey = \\Laptop.model\n\t...\n}\n```\n\n\u003e 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 🤘\n\n### 2 Create Stores\n\n```swift\n// To save objects to UserDefaults, create UserDefaultsStore:\nlet usersStore = UserDefaultsStore\u003cUser\u003e(uniqueIdentifier: \"users\")!\nlet laptopsStore = UserDefaultsStore\u003cLaptop\u003e(uniqueIdentifier: \"laptops\")!\n\n// To save a single object to UserDefaults, create UserDefaultsStore:\nlet userStore = SingleUserDefaultsStore\u003cUser\u003e(uniqueIdentifier: \"user\")!\n\n// If you want to share data between app and extentions:\nlet sharedUsersStore = UserDefaultsStore\u003cUser\u003e(uniqueIdentifier: \"users\", groupIdentifier: \"com.yourCompany.app\")!\nlet sharedUserStore = SingleUserDefaultsStore\u003cUser\u003e(uniqueIdentifier: \"user\", groupIdentifier: \"com.yourCompany.app\")!\n\n// To save objects to the file system, create FilesStore:\nlet usersStore = FilesStore\u003cUser\u003e(uniqueIdentifier: \"users\")\nlet laptopsStore = FilesStore\u003cLaptop\u003e(uniqueIdentifier: \"laptops\")\n\n// To save objects to the app group shared file system, create FilesStore:\nlet appGroup = Bundle.main.infoDictionary?[\"appGroup\"] as? String ?? \"group.company.app\"\nlet usersStore = FilesStore\u003cUser\u003e(uniqueIdentifier: \"users\", groupIdentifier: appGroup)\nlet laptopsStore = FilesStore\u003cLaptop\u003e(uniqueIdentifier: \"laptops\", groupIdentifier: appGroup)\n\n// To save a single object to the file system, create SingleFilesStore:\nlet userStore = SingleFilesStore\u003cUser\u003e(uniqueIdentifier: \"user\")\n\n// To save a single object to the app group shared file system, create SingleFilesStore:\nlet appGroup = Bundle.main.infoDictionary?[\"appGroup\"] as? String ?? \"group.company.app\"\nlet userStore = SingleFilesStore\u003cUser\u003e(uniqueIdentifier: \"user\", groupIdentifier: appGroup)\n\n// To save a single object to the system's keychain, create SingleKeychainStore:\nlet userStore = SingleKeychainStore\u003cUser\u003e(uniqueIdentifier: \"user\")\n```\n\n\n### 3. Voilà, you're all set!\n\n```swift\nlet macbook = Laptop(model: \"A1278\", name: \"MacBook Pro\")\nlet john = User(userId: 1, firstName: \"John\", lastName: \"Appleseed\", laptop: macbook)\n\n// Save an object to a store\ntry! usersStore.save(john)\n\n// Save an array of objects to a store\ntry! usersStore.save([jane, steve, jessica])\n\n// Get an object from store\nlet user = store.object(withId: 1)\nlet laptop = store.object(withId: \"A1278\")\n\n// Get all objects in a store\nlet laptops = laptopsStore.allObjects()\n\n// Check if store has an object\nprint(usersStore.hasObject(withId: 10)) // false\n\n// Iterate over all objects in a store\nlaptopsStore.forEach { laptop in\n\tprint(laptop.name)\n}\n\n// Delete an object from a store\nusersStore.delete(withId: 1)\n\n// Delete all objects in a store\nlaptops.deleteAll()\n\n// Know how many objects are stored in a store\nlet usersCount = usersStore.objectsCount\n```\n\n## Requirements\n\n- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+\n- Xcode 10.0+\n- Swift 4.2+\n\n\n## Thanks\n\nSpecial thanks to:\n- [Paul Hudson](https://twitter.com/twostraws) for his [article](https://www.hackingwithswift.com/articles/57/how-swift-keypaths-let-us-write-more-natural-code) on how to use Swift keypaths to write more natural code.\n\n## Credits\n- [Icon](https://www.flaticon.com/free-icon/box_1198446) made by [freepik](https://www.flaticon.com/authors/freepik) from [flaticon.com](https://www.flaticon.com).\n\n\n## License\n\nPersistenceKit is released under the MIT license. See [LICENSE](https://github.com/Teknasyon-Teknoloji/PersistenceKit/blob/master/LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTeknasyon-Teknoloji%2FPersistenceKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTeknasyon-Teknoloji%2FPersistenceKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTeknasyon-Teknoloji%2FPersistenceKit/lists"}