{"id":19440508,"url":"https://github.com/felilo/swiftrecord","last_synced_at":"2025-09-02T08:33:51.266Z","repository":{"id":94170066,"uuid":"292074800","full_name":"felilo/SwiftRecord","owner":"felilo","description":"Fork SwiftRecord-riptec","archived":false,"fork":false,"pushed_at":"2020-09-02T16:33:43.000Z","size":246,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-01-07T21:46:54.049Z","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/felilo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-01T18:20:14.000Z","updated_at":"2020-09-02T16:33:45.000Z","dependencies_parsed_at":"2023-03-17T22:30:27.191Z","dependency_job_id":null,"html_url":"https://github.com/felilo/SwiftRecord","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felilo%2FSwiftRecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felilo%2FSwiftRecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felilo%2FSwiftRecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felilo%2FSwiftRecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felilo","download_url":"https://codeload.github.com/felilo/SwiftRecord/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240621298,"owners_count":19830511,"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-11-10T15:29:31.501Z","updated_at":"2025-02-25T07:26:28.663Z","avatar_url":"https://github.com/felilo.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftRecord [![CocoaPod][pd-bdg]][pd] [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT) [![Issues](http://img.shields.io/github/issues/arkverse/SwiftRecord.svg)]( https://github.com/arkverse/SwiftRecord/issues)\n[pd-bdg]: https://img.shields.io/cocoapods/v/SwiftRecord.svg\n[pd]: http://cocoadocs.org/docsets/SwiftRecord\nby [ark](http://www.arkverse.com). tweet [@arkverse](https://twitter.com/arkverse) for any feature requests. Feedback is greatly appreciated!\n\n## About\n\nActiveRecord style Core Data object management. Tremendously convenient and easy to use. Necessary for any and every Core Data project.\n\nWritten purely in Swift and based heavily on [ObjectiveRecord](https://github.com/supermarin/ObjectiveRecord)\n\nEasy creates, saves, deletes and queries. Do it using:\n\n- `[String:AnyObject]` dictionaries for creates, queries or sorts\n- `String` for queries and sorts, ie `name == 'someName'` or `date ASC`\n- `NSPredicate` and `NSSortDescriptor`/`[NSSortDescriptor]` for queries and sorts if you'd like\n\nNow supports setting an autoincrementing ID for your `NSManagedObject` subclasses\n\nThis library also reads in your json dictionaries for you. Includes automatic camelCase changing ie `first_name` from server to `firstName` locally. You can customize the dictionary mappings too. Read more in the [mapping section](#mapping).\n\nObject relationships are also generated from dictionaries, but disabled by default. Set `SwiftRecord.generateRelationships` to true to enable this feature. See the [relationships section](#relationships)\n\nVisit [ark](http://www.arkverse.com) for a more [beginner friendly guide to SwiftRecord](http://www.arkverse.com/swiftrecord-easy-core-data-written-in-swift/)\n\nif you love SwiftRecord, tweet it!\n\u003ca href=\"https://twitter.com/intent/tweet?text=SwiftRecord%3A+an+amazing+swift+Core+Data+library\u0026url=https%3A%2F%2Fgithub.com%2Farkverse%2FSwiftRecord\u0026hashtags=ios%2Cswift%2Ccoredata\u0026original_referer=http%3A%2F%2Fgithub.com%2F\u0026tw_p=tweetbutton\" target=\"_blank\"\u003e\n  \u003cimg src=\"http://jpillora.com/github-twitter-button/img/tweet.png\"\n       alt=\"tweet button\" title=\"SwiftRecord: an amazing swift Core Data library\"\u003e\u003c/img\u003e\n\u003c/a\u003e\n\n## Table of Contents\n\n1. [Installation](#installation)\n2. [Creates, Saves, Deletes](#create,)\n3. [querying](#querying)\n4. [sorting, limits](#sorting, limits)\n5. [aggregation](#aggregation)\n6. [custom coredata](#custom core data)\n7. [mapping](#mapping)\n8. [relationships](#relationships)\n9. [autoincrement](#autoincrement)\n\n## Installation\n\n#### via [CocoaPods](http://cocoapods.org)\n1. Edit your Podfile to use frameworks and add SwiftRecord:\n\n\t\tplatform :ios, '8.0'\n\t\tuse_frameworks!\n\n\t\tpod 'SwiftRecord'\n2. run `pod install`\n\n#### via Carthage\n\n1. Just add SwiftRecord to your Cartfile:\n\n\tgithub \"arkverse/SwiftRecord\" \u003e= 0.0.6\n\n2. and run `carthage update`\n\n#### Manual Installation\nDrag and drop either `Classes/SwiftRecord.swift` or `SwiftRecord.framework` into your project\n\n## Usage\n\n1. At the top of every `NSManagedObject` swift class add:\n\n\t```swift\n\timport SwiftRecord\n\n\t@objc(Event)\n\tclass Event: NSManagedObject {\n\t\t@NSManaged var eventID: NSNumber\n\t\t@NSManaged var name: String\n\t\t@NSManaged var type: String\n\t\t@NSManaged var when: NSDate\n\t\t@NSManaged var creator: User\n\t\t@NSManaged var attendess: NSSet\n\t}\n\t```\n2. Set up Core Data by naming your model file `MyAppName.xcdatamodel`. Optionally, you can set your own model name by calling `SwiftRecored.sharedRecord.modelName = \"mymodelname\"`\n\n### create, save \u0026 delete:\n\n```swift\nvar event = Event.create() as! Event // Downcasts are necessary,\nevent.type = \"Birthday\"\nevent.when = NSDate()\nevent.save() // simple save\nevent.delete() // simple delete\n\nvar properties: [String:AnyObject] = [\"name\":\"productQA\",\"type\":\"meeting\", \"when\":NSDate()]\nvar meeting = Event.create(properties) as! Event // Remember to downcast\n```\n\nNote: the downside to using swift is that `NSManagedObject` and `[NSManagedObject]`(arrays) are returned*, so api calls have to down casted into your class type. Feel free to always force downcast. `MyObject` calls will always create `MyObject` unless your class could not be found\n\n*`instancetype`, `Self` in Swift, has only just been added, and still pretty useless\n\n### querying\n\nEasily query against your entities. Queries accept optional condition, sort and limit parameters.\n\n```swift\n// grab all Events\nvar events = Event.all() as! [Event]\n\n// all past events before now\nvar pastEvents = Event.query(\"when \u003c %@\", NSDate()) as! [Event]\n\n// specific event, yes we have support for format\u0026arguments. Note, finding specific events return optional vars\nvar thisEvent = Event.find(\"name == %@ AND when == %@\", \"productQA\", NSDate()) as? Event\n\n// Use dictionaries to query too\nvar birthdayEvents = Event.query([\"type\":\"birthday\"]) as! [Event]\n\n// or NSPredicates\nvar predicate = NSPredicate(\"type == %@\", \"meeting\")\nvar meetingEvents = Event.query(predicate) as! [Event]\n```\n\n### sorting, limits\n\n```swift\n// Events sorted by date, defaults to ascending\nvar events = Event.all(sort: \"when\") as! [Event]\n// Descending\nvar descendingEvents = Event.all(sort:[\"when\":\"DESC\"]) as! [Event]\n// or\nvar descEvents = Event.all(sort:\"when DESC, eventID ASC\")\n\n// All meeting events sorted by when desc and eventID ascending and limit 10\nvar theseEvents = Event.query([\"type\":\"meeting\"], sort:[\"when\":\"DESC\",\"eventID\":\"ASC\"], limit: 10) as! [Event]\n\n// NSSortDescriptor as sort arg (or array of NSSortDescriptors\nEvent.all(sort: NSSortDescriptor(key:\"when\",ascending:true))\n```\n\n### Aggregation\n\n```swift\n// count all Events\nvar count = Event.count()\n\n// count meeting Events\nvar count = Event.count(\"type == 'meeting'\")\n```\n\n### Custom Core Data\n\n#### Custom ManagedObjectContext\nif you made your own, feel free to set it\n\n```swift\nvar myContext: NSManagedObjectContext = ...\n\nSwiftRecord.sharedRecord.managedObjectContext = myContext\n```\n\n#### Custom CoreData model or .sqlite database\nDon't set these if you set your own context.\nIf you have a modelName different from the name of your app, set it. If you want a different databse name, set it.\n```swift\nSwiftRecord.sharedRecord.modelName = \"MyModelName\"\nSwiftRecord.sharedRecord.databaseName = \"custom_database_name\"\n```\n\n### Mapping\n\nThe most of the time, your JSON web service returns keys like `first_name`, `last_name`, etc. \u003cbr/\u003e\nYour Swift implementation has camelCased properties - `firstName`, `lastName`.\u003cbr/\u003e\n\nWe automatically check against camelCase variations.\n\nIf you have different variations, override `static func mappings() -\u003e [String:String]` to specify your local to remote key mapping\n\nThe key string is your local name, the value string, your remote name.\n\n```swift\n// this method is called once, so you don't have to do any caching / singletons\nclass Event: NSManagedObject {\n\noverride class func mappings() -\u003e [String:String] {\n\treturn [\"localName\":\"remoteName\",\"eventID\":\"_id\",\"attendees\":\"people\"]\n}\n  // firstName =\u003e first_name is automatically handled\n}\n\n@end\n```\n\n### Relationships\nWhile it is advised against, you can have your NSManagedObject relationships in your dictionaries and they will be filled, but first you must enable it by setting:\n\n\tSwiftRecord.generateRelationships = true\n\nOnce this is set, you rels will be filled, ie:\n\n```swift\nvar personDict = [\"email\":\"zaid@arkverse.com\",\"firstName\":\"zaid\"]\nvar eventDict = [\"name\":\"product QA meeting\",\"when\":NSDate(),\"creator\":personDict]\nvar event = Event.create(eventDict) as! Event\nvar person: Person = event.creator\nprintln(person.username)\n```\n\n### Autoincrement\nIf you would like SwiftRecord to manage autoincrementing your ID property for you, enable it just by override autoIncrementingId() and return the key for your ID property:\n\n```swift\npublic class Event: NSManagedObject {\n\n   override public func autoIncrementingId() -\u003e String? {\n       return \"eventId\"\n   }\n```\nNote, this will set your ID property whenever one isn't provided. You will have duplicate ID's if you set your own objects id. For example, autoincrement is at ID 150 and you create an object with ID 200, autoincrement will eventually create an object with ID 200 as well. Enforcing uniqueness in Core Data is too expensive.\n\n#### Testing\n\nObjectiveRecord supports CoreData's in-memory store. In any place, before your tests start running, it's enough to call\n```swift\nSwiftRecord.sharedRecord.useInMemoryStore()\n```\n\n#### Roadmap\n\n- improve Swiftiness\n- JSON generation\n- Possible add NSOrderedSet support in Relationship init\n- Support Realm\n- Better typing\n\n## License\n\nSwiftRecord is available under the MIT license. See the LICENSE file\nfor more information.\n\nCheck out [ark](http://www.arkverse.com) for more about us\n\n![ga tracker](https://www.google-analytics.com/collect?v=1\u0026a=257770996\u0026t=pageview\u0026dl=https%3A%2F%2Fgithub.com%2Farkverse%2FSwiftRecord\u0026ul=en-us\u0026de=UTF-8\u0026cid=978224512.1377738459\u0026tid=UA-63011921-1\u0026z=887657232 \"ga tracker\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelilo%2Fswiftrecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelilo%2Fswiftrecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelilo%2Fswiftrecord/lists"}