{"id":979,"url":"https://github.com/modo-studio/SugarRecord","last_synced_at":"2025-08-06T13:32:05.109Z","repository":{"id":19356851,"uuid":"22596493","full_name":"modo-studio/SugarRecord","owner":"modo-studio","description":"CoreData/Realm sweet wrapper written in Swift","archived":false,"fork":false,"pushed_at":"2020-09-10T07:39:54.000Z","size":157364,"stargazers_count":2110,"open_issues_count":17,"forks_count":223,"subscribers_count":58,"default_branch":"master","last_synced_at":"2024-11-30T17:03:03.030Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/modo-studio.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":"2014-08-04T08:05:03.000Z","updated_at":"2024-11-06T10:39:10.000Z","dependencies_parsed_at":"2022-09-10T20:31:20.395Z","dependency_job_id":null,"html_url":"https://github.com/modo-studio/SugarRecord","commit_stats":null,"previous_names":["carambalabs/sugarrecord"],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modo-studio%2FSugarRecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modo-studio%2FSugarRecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modo-studio%2FSugarRecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modo-studio%2FSugarRecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/modo-studio","download_url":"https://codeload.github.com/modo-studio/SugarRecord/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228633914,"owners_count":17949161,"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:36.156Z","updated_at":"2024-12-09T14:30:44.253Z","avatar_url":"https://github.com/modo-studio.png","language":"Swift","readme":"# \u003ccenter\u003e![SugarRecord](Assets/Caramba.png)\u003c/center\u003e\n\n[![Twitter: @carambalabs](https://img.shields.io/badge/contact-@carambalabs-blue.svg?style=flat)](https://twitter.com/carambalabs)\n[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/SugarRecord.svg)](https://img.shields.io/cocoapods/v/SugarRecord.svg)\n[![Language: Swift](https://img.shields.io/badge/lang-Swift-yellow.svg?style=flat)](https://developer.apple.com/swift/)\n[![Language: Swift](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](http://opensource.org/licenses/MIT)\n[![Build Status](https://travis-ci.org/carambalabs/SugarRecord.svg)](https://travis-ci.org/carambalabs/SugarRecord)\n[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\n## What is SugarRecord?\nSugarRecord is a persistence wrapper designed to make working with persistence solutions like CoreData in a much easier way. Thanks to SugarRecord you'll be able to use CoreData with just a few lines of code: Just choose your stack and start playing with your data.\n\nThe library is maintained by [@carambalabs](https://github.com/carambalabs). You can reach me at [pepibumur@gmail.com](mailto://pepibumur@gmail.com) for help or whatever you need to commend about the library.\n\n[![paypal](https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=2AUKNEW4JLPXQ)\n\n\n## Features\n- Swift 3.0 compatible (Xcode 8.0).\n- Protocols based design.\n- For **beginners** and **advanced** users\n- Fully customizable. Build your own stack!\n- Friendly syntax (fluent)\n- Away from Singleton patterns! No shared states :tada:\n- Compatible with OSX/iOS/watchOS/tvOS\n- Fully tested (thanks Nimble and Quick)\n- Actively supported\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 'SugarRecord'`\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**Available specs**\nChoose the right one depending ton the configuration you need for you app.\n\n```ruby\npod \"SugarRecord/CoreData\"\npod \"SugarRecord/CoreData+iCloud\"\n```\n\n### [Carthage](https://github.com/carthage)\n\n1. Install [Carthage](https://github.com/carthage). You can do it with `brew install carthage`.\n2. Edit your `Cartfile` file and add the following line `github \"carambalabs/sugarrecord\".\n3. Execute `carthage update`\n4. Add the frameworks to your project as explained on the [Carthage repository](https://github.com/carthage).\n\n## Reference\nYou can check generated SugarRecord documentation [here](http://cocoadocs.org/docsets/SugarRecord/2.0.0/) generated automatically with [CocoaDocs](http://cocoadocs.org/)\n\n# How to use\n\n#### Creating your Storage\nA storage represents your database. The first step to start using SugarRecord is initializing the storage. SugarRecord provides a default storages, `CoreDataDefaultStorage`.\n\n```swift\n// Initializing CoreDataDefaultStorage\nfunc coreDataStorage() -\u003e CoreDataDefaultStorage {\n    let store = CoreDataStore.named(\"db\")\n    let bundle = Bundle(for: self.classForCoder)\n    let model = CoreDataObjectModel.merged([bundle])\n    let defaultStorage = try! CoreDataDefaultStorage(store: store, model: model)\n    return defaultStorage\n}\n```\n\n##### Creating an iCloud Storage\n\nSugarRecord supports the integration of CoreData with iCloud. It's very easy to setup since it's implemented in its own storage that you can use from your app, `CoreDataiCloudStorage`:\n\n```swift\n// Initializes the CoreDataiCloudStorage\nfunc icloudStorage() -\u003e CoreDataiCloudStorage {\n    let bundle = Bundle(for: self.classForCoder)\n    let model = CoreDataObjectModel.merged([bundle])\n    let icloudConfig = CoreDataiCloudConfig(ubiquitousContentName: \"MyDb\", ubiquitousContentURL: \"Path/\", ubiquitousContainerIdentifier: \"com.company.MyApp.anothercontainer\")\n    let icloudStorage = try! CoreDataiCloudStorage(model: model, iCloud: icloudConfig)\n    return icloudStorage\n}\n```\n\n#### Contexts\nStorages offer multiple kind of contexts that are the entry points to the database. For curious developers, in case of CoreData a context is a wrapper around `NSManagedObjectContext`. The available contexts are:\n\n- **MainContext:** Use it for main thread operations, for example fetches whose data will be presented in the UI.\n- **SaveContext:** Use this context for background operations. The context is initialized when the storage instance is created. That context is used for storage operations.\n- **MemoryContext:** Use this context when you want to do some tests and you don't want your changes to be persisted.\n\n#### Fetching data\n\n```swift\nlet pedros: [Person] = try! db.fetch(FetchRequest\u003cPerson\u003e().filtered(with: \"name\", equalTo: \"Pedro\"))\nlet tasks: [Task] = try! db.fetch(FetchRequest\u003cTask\u003e())\nlet citiesByName: [City] = try! db.fetch(FetchRequest\u003cCity\u003e().sorted(with: \"name\", ascending: true))\nlet predicate: NSPredicate = NSPredicate(format: \"id == %@\", \"AAAA\")\nlet john: User? = try! db.fetch(FetchRequest\u003cUser\u003e().filtered(with: predicate)).first\n```\n\n#### Remove/Insert/Update operations\n\nAlthough `Context`s offer `insertion` and `deletion` methods that you can use it directly SugarRecords aims at using the `operation` method method provided by the storage for operations that imply modifications of the database models:\n\n- **Context**: You can use it for fetching, inserting, deleting. Whatever you need to do with your data.\n- **Save**: All the changes you apply to that context are in a memory state unless you call the `save()` method. That method will persist the changes to your store and propagate them across all the available contexts.\n\n```swift\ndo {\n  db.operation { (context, save) throws in\n    // Do your operations here\n    try save()\n  }\n} catch {\n  // There was an error in the operation\n}\n```\n\n##### New model\nYou can use the context `new()` method to initialize a model **without inserting it in the context**:\n\n```swift\ndo {\n  db.operation { (context, save) throws in\n    let newTask: Track = try context.new()\n    newTask.name = \"Make CoreData easier!\"\n    try context.insert(newTask)\n    try save()\n  }\n} catch {\n  // There was an error in the operation\n}\n```\n\u003e In order to insert the model into the context you use the insert() method.\n\n##### Creating a model\nYou can use the `create()` for initializing and inserting in the context in the same operation:\n\n```swift\ndo {\n  db.operation { (context, save) throws -\u003e Void in\n    let newTask: Track = try! context.create()\n    newTask.name = \"Make CoreData easier!\"\n    save()\n  }\n}\ncatch {\n  // There was an error in the operation\n}\n```\n\n##### Delete a model\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  db.operation { (context, save) throws in\n    let john: User? = try context.request(User.self).filteredWith(\"id\", equalTo: \"1234\").fetch().first\n    if let john = john {\n      try context.remove([john])\n      try save()\n    }\n  }\n} catch {\n  // There was an error in the operation\n}\n```\n\n\u003cbr\u003e\n\u003e This is the first approach of SugarRecord for the  interface. We'll improve it with the feedback you can report and according to the use of the framework. Do not hesitate to reach us with your proposals. Everything that has to be with making the use of CoreData easier, funnier, and enjoyable is welcome! :tada:\n\n### RequestObservable\n\nSugarRecord provides a component, `RequestObservable` that allows observing changes in the DataBase. It uses `NSFetchedResultsController` under the hood.\n\n**Observing**\n\n```swift\nclass Presenter {\n  var observable: RequestObservable\u003cTrack\u003e!\n\n  func setup() {\n      let request: FetchRequest\u003cTrack\u003e = FetchRequest\u003cTrack\u003e().filtered(with: \"artist\", equalTo: \"pedro\")\n      self.observable = storage.instance.observable(request)\n      self.observable.observe { changes in\n        case .Initial(let objects):\n          print(\"\\(objects.count) objects in the database\")\n        case .Update(let deletions, let insertions, let modifications):\n          print(\"\\(deletions.count) deleted | \\(insertions.count) inserted | \\(modifications.count) modified\")\n        case .Error(let error):\n          print(\"Something went wrong\")\n      }\n  }\n}\n```\n\u003e **Retain**: RequestObservable must be retained during the observation lifecycle. When the `RequestObservable` instance gets released from memory it stops observing changes from your storage.\n\n\u003e **NOTE**: This was renamed from Observable -\u003e RequestObservable so we are no longer stomping on the RxSwift Observable namespace.\n\n**:warning: `RequestObservable` is only available for CoreData + OSX since MacOS 10.12**\n\n## Resources\n- [Quick](https://github.com/quick/quick)\n- [Nimble](https://github.com/quick/nimble)\n- [CoreData and threads with GCD](http://www.cimgf.com/2011/05/04/core-data-and-threads-without-the-headache/)\n- [Jazzy](https://github.com/realm/jazzy)\n- [iCloud + CoreData (objc.io)](http://www.objc.io/issue-10/icloud-core-data.html)\n\n## Contributors\n\n[\u003cimg alt=\"glebo\" src=\"https://avatars3.githubusercontent.com/u/3298239?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/glebo)[\u003cimg alt=\"sushichop\" src=\"https://avatars3.githubusercontent.com/u/5669641?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/sushichop)[\u003cimg alt=\"foxling\" src=\"https://avatars3.githubusercontent.com/u/506125?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/foxling)[\u003cimg alt=\"ZevEisenberg\" src=\"https://avatars2.githubusercontent.com/u/464574?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/ZevEisenberg)[\u003cimg alt=\"konyu\" src=\"https://avatars2.githubusercontent.com/u/1217706?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/konyu)\n\n[\u003cimg alt=\"yuuki1224\" src=\"https://avatars2.githubusercontent.com/u/1756640?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/yuuki1224)[\u003cimg alt=\"YTN01\" src=\"https://avatars3.githubusercontent.com/u/5421955?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/YTN01)[\u003cimg alt=\"gitter-badger\" src=\"https://avatars2.githubusercontent.com/u/8518239?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/gitter-badger)[\u003cimg alt=\"sergigracia\" src=\"https://avatars3.githubusercontent.com/u/1061658?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/sergigracia)[\u003cimg alt=\"adityatrivedi\" src=\"https://avatars0.githubusercontent.com/u/1791760?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/adityatrivedi)\n\n[\u003cimg alt=\"Adlai-Holler\" src=\"https://avatars2.githubusercontent.com/u/2466893?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/Adlai-Holler)[\u003cimg alt=\"akshaynhegde\" src=\"https://avatars0.githubusercontent.com/u/3814615?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/akshaynhegde)[\u003cimg alt=\"goingreen\" src=\"https://avatars2.githubusercontent.com/u/16046834?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/goingreen)[\u003cimg alt=\"startupthekid\" src=\"https://avatars3.githubusercontent.com/u/3139429?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/startupthekid)[\u003cimg alt=\"ctotheameron\" src=\"https://avatars0.githubusercontent.com/u/3002968?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/ctotheameron)\n\n[\u003cimg alt=\"davidahouse\" src=\"https://avatars2.githubusercontent.com/u/140127?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/davidahouse)[\u003cimg alt=\"A8-Moke\" src=\"https://avatars2.githubusercontent.com/u/12935988?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/A8-Moke)[\u003cimg alt=\"Arasthel\" src=\"https://avatars1.githubusercontent.com/u/480955?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/Arasthel)[\u003cimg alt=\"LuizZak\" src=\"https://avatars3.githubusercontent.com/u/6502879?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/LuizZak)[\u003cimg alt=\"literator\" src=\"https://avatars1.githubusercontent.com/u/242131?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/literator)\n\n[\u003cimg alt=\"madeinqc\" src=\"https://avatars0.githubusercontent.com/u/7191124?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/madeinqc)[\u003cimg alt=\"kolisko\" src=\"https://avatars0.githubusercontent.com/u/460056?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/kolisko)[\u003cimg alt=\"dukemike\" src=\"https://avatars3.githubusercontent.com/u/11562781?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/dukemike)[\u003cimg alt=\"rafalwojcik\" src=\"https://avatars3.githubusercontent.com/u/512353?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/rafalwojcik)[\u003cimg alt=\"thad\" src=\"https://avatars1.githubusercontent.com/u/139789?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/thad)\n\n[\u003cimg alt=\"chrispix\" src=\"https://avatars0.githubusercontent.com/u/190962?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/chrispix)[\u003cimg alt=\"ReadmeCritic\" src=\"https://avatars3.githubusercontent.com/u/15367484?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/ReadmeCritic)[\u003cimg alt=\"avielg\" src=\"https://avatars3.githubusercontent.com/u/5012557?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/avielg)[\u003cimg alt=\"rdougan\" src=\"https://avatars3.githubusercontent.com/u/28582?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/rdougan)[\u003cimg alt=\"grangej\" src=\"https://avatars0.githubusercontent.com/u/604788?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/grangej)\n\n[\u003cimg alt=\"fjbelchi\" src=\"https://avatars2.githubusercontent.com/u/626713?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/fjbelchi)[\u003cimg alt=\"dcvz\" src=\"https://avatars0.githubusercontent.com/u/2475932?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/dcvz)[\u003cimg alt=\"pepibumur\" src=\"https://avatars3.githubusercontent.com/u/663605?v=4\u0026s=117\" width=\"117\"\u003e](https://github.com/pepibumur)\n\n## About\n\n\u003cimg src=\"https://github.com/carambalabs/Foundation/blob/master/ASSETS/logo-salmon.png?raw=true\" width=\"200\" /\u003e\n\nThis project is funded and maintained by [Caramba](http://caramba.io). We 💛 open source software!\n\nCheck out our other [open source projects](https://github.com/carambalabs/), read our [blog](http://blog.caramba.io) or say :wave: on twitter [@carambalabs](http://twitter.com/carambalabs).\n\n## Contribute\n\nContributions are welcome :metal: We encourage developers like you to help us improve the projects we've shared with the community. Please see the [Contributing Guide](https://github.com/carambalabs/Foundation/blob/master/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/carambalabs/Foundation/blob/master/CONDUCT.md).\n\n## License\nThe MIT License (MIT)\n\nCopyright (c) 2017 Caramba\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","funding_links":["https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif","https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=2AUKNEW4JLPXQ"],"categories":["Database","Libs","Data Management [🔝](#readme)","etc"],"sub_categories":["Getting Started","Data Management","Linter"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodo-studio%2FSugarRecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmodo-studio%2FSugarRecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodo-studio%2FSugarRecord/lists"}