{"id":956,"url":"https://github.com/mrdekk/DataKernel","last_synced_at":"2025-08-06T13:32:07.466Z","repository":{"id":56908232,"uuid":"57988044","full_name":"mrdekk/DataKernel","owner":"mrdekk","description":"Simple CoreData wrapper to ease operations","archived":false,"fork":false,"pushed_at":"2019-05-14T06:39:01.000Z","size":82,"stargazers_count":18,"open_issues_count":7,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-06T21:09:24.321Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/mrdekk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-03T17:30:37.000Z","updated_at":"2024-07-28T07:52:09.000Z","dependencies_parsed_at":"2022-08-21T03:21:00.333Z","dependency_job_id":null,"html_url":"https://github.com/mrdekk/DataKernel","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdekk%2FDataKernel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdekk%2FDataKernel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdekk%2FDataKernel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdekk%2FDataKernel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrdekk","download_url":"https://codeload.github.com/mrdekk/DataKernel/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":[],"created_at":"2024-01-05T20:15:35.519Z","updated_at":"2024-12-09T14:30:46.492Z","avatar_url":"https://github.com/mrdekk.png","language":"Swift","funding_links":[],"categories":["Core Data","Libs","Data Management [🔝](#readme)"],"sub_categories":["Linter","Data Management","Other free courses"],"readme":"# DataKernel\n\n[![Language: Swift](https://img.shields.io/badge/lang-Swift-orange.svg?style=flat)](https://developer.apple.com/swift/)\n[![Language: Swift](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](http://opensource.org/licenses/MIT)\n[![Build Status](https://travis-ci.org/mrdekk/DataKernel.svg?branch=master)](https://travis-ci.org/mrdekk/DataKernel)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods compatible](https://img.shields.io/badge/Cocoa_Pods-compatible-4BC51D.svg?style=flat)](http://cocoapods.org)\n\n## What is DataKernel?\n\nDataKernel is a minimalistic wrapper around CoreData stack to ease persistence operations. It is heavily inspired by [SugarRecord][site-sugarrecord] but have no external dependencies (except Cocoa of course) and with some refinements. It is covered with unit tests.\n\n[site-sugarrecord]: https://github.com/pepibumur/SugarRecord\n\nIf you have any propositions please file issue.\nIf you need usage examples - see unit test, it is very straightforward\n\n## Features\n- Swift (tested in XCode 9.1)\n- Protocols based design\n- Fully tested\n- Actively supported\n- Rich set of operations (but I think something may be missed)\n- No iCloud yet (but planned)\n\n## Note\n\n- context.wipe uses NSBatchDeleteRequest on iOS 9 and OSX 10.11, it gives big performance improvements, but can cause problems if you use patterns like wipe all and create new, because it acts directly on persistent store (see Apple WWDC 2015 video or slides)\n\n## Setup\n\n### [CocoaPods](https://cocoapods.org)\n\n1. Install [CocoaPods](https://cocoapods.org). You can do it with `gem install cocoapods`\n2. Edit your `Podfile` file and add the following line `pod 'DataKernel'` (you have to use use_frameworks!, because this is a Swift pod)\n3. Update your pods with the command `pod install`\n4. Open the project from the generated workspace (`.xcworkspace` file).\n\n*Note: You can also test the last commits by specifying it directly in the Podfile line*\n\n### [Carthage](https://carthage)\n1. Install [Carthage](https://github.com/carthage/carthage) on your computer using `brew install carthage`\n3. Edit your `Cartfile` file adding the following line `github \"mrdekk/DataKernel\"`\n4. Update and build frameworks with `carthage update`\n5. Add generated frameworks to your app main target following the steps [here](https://github.com/carthage/carthage)\n6. Link your target with **CoreData** library *(from Build Phases)*\n\n# How to use\n\n#### Creating your Storage\n\nA storage is a general wrapper around CoreData PersistentStoreCoordination and PersistentStores. The first step you need to get started is to create `CoreDataLocalStorage`.\n\n```swift\nlet store = StoreRef.Named(\"test1\")\nlet bundle = NSBundle(forClass: self.classForCoder)\nlet model = ModelRef.Merged([bundle])\nlet storage = try! CoreDataLocalStorage(store: store!, model: model!, migration: true)\n```\n\n#### Contexts\n\nStorage offer access to uiContext (NSManagedObjectContext on main thread to **GET** data from storage). All modification operations on CoreData entities should be performed with `perform` operation on storage. \n\n#### Fetching data\n\n```swift\nlet cars: [Cars] = try! storage.uiContext.fetch(Request\u003cCar\u003e().filtered(\"mark\", equalTo: \"Honda\"))\nlet cars: [Cars] = try! storage.uiContext.fetch(Request\u003cCar\u003e())\nlet cars: [Cars] = try! storage.uiContext.fetch(Request\u003cCar\u003e().sorted(\"model\", ascending: true))\n\nlet predicate = NSPredicate(format: \"model == %@\", \"CRZ\")\nlet crz: Car? = try! storage.uiContext.fetch(Request\u003cCar\u003e(predicate: predicate)).first\n```\n\n#### Remove/Insert/Update operations\n\nAll modification operations should be performed under `perform` operation due to it handles all core data context/threading things internally and you can safely use modification operations under them.\n\n`save` operation perform recursive save with nesting context on core data stack. So, if you change entities that already loaded in uiContext, they will be updated.\n\nNote the first parameter `ephemeral` on `perform` function. If it is true, new context for this operation will be created, and then after save succeeded it will be removed. Creation of contexts in CoreData is rather cheap operations, so don't worry. If you don't need to save data in ephemeral context, just don't call `save`. If you set `ephemeral = false` then a pre-created special save context will be used for all `ephemeral = false` operations.\n\n```swift\ndo {\n  storage.perform(true) { (context, save) throws -\u003e Void in\n    // do you unit of work here\n    save()\n  }\n}\ncatch {\n  // There was an error in the operation\n}\n```\n\n##### Creating a model\n\nYou can use the `create()` for initializing and inserting in the context in the same operation:\n\n```swift\ndo {\n  storage.perform(true) { (context, save) throws -\u003e Void in\n    let newCar: Car = try! context.create()\n    newCar.model = \"Honda\"\n    newCar.mark = \"CRZ\"\n    save()\n  }\n}\ncatch {\n  // There was an error in the operation\n}\n```\n\n#### Upsert a model \n\nUpsert - update or insert first search the entity in storage, and then if it is not found creates it. Just call the `acquire` function.\n\nImportant: to get this feature work, you should add model property `pk` and set it to name of the field, that is primary key of this entity. Moreover, this property should be indexed. It is needed to achieve needed level of performance.\n\n```swift\ndo {\n  storage.perform(true) { (context, save) throws -\u003e Void in\n    let car: Car = try! context.acquire(\"CRZ\")\n  }\n}\ncatch {\n  // There was an error in the operation\n}\n```\n\n##### Delete a model\n\nIn a similar way you can use the `remove()` method from the context passing the objects you want to remove from the database:\n\n```swift\ndo {\n  storage.perform(true) { (context, save) throws -\u003e Void in\n    let car: Car? = try! context.fetch(Request\u003cCar\u003e.filtered(\"model\", equalTo: \"CRZ\")).first\n    if let car = car {\n      try! context.remove([car])\n      save()\n    }\n  }\n}\ncatch {\n  // There was an error in the operation\n}\n```\n\n# Contributing\n\n## Support\n\nIf you want to communicate any issue, suggestion or even make a contribution, you have to keep in mind the flow bellow:\n\n- If you need help, ask your doubt in Stack Overflow using the tag 'datakernel'\n- If you want to ask something in general, use Stack Overflow too.\n- Open an issue either when you have an error to report or a feature request.\n- If you want to contribute, submit a pull request, and remember the rules to follow related with the code style, testing, ...\n\n## Code of conduct\n\nThis project adheres to the [Open Code of Conduct][code-of-conduct]. By participating, you are expected to honor this code.\n[code-of-conduct]: http://todogroup.org/opencodeofconduct/#DataKernel/mrdekk@yandex.ru\n\n## License\nThe MIT License (MIT)\n\nCopyright (c) \u003c2017\u003e \u003cMrDekk\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrdekk%2FDataKernel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrdekk%2FDataKernel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrdekk%2FDataKernel/lists"}