by ark. tweet @arkverse for any feature requests. Feedback is greatly appreciated!
##About
ActiveRecord style Core Data object management. Tremendously convenient and easy to use. Necessary for any and every Core Data project.
Written purely in Swift and based heavily on ObjectiveRecord
Easy creates, saves, deletes and queries. Do it using:
[String:AnyObject]
dictionaries for creates, queries or sortsString
for queries and sorts, iename == 'someName'
ordate ASC
NSPredicate
andNSSortDescriptor
/[NSSortDescriptor]
for queries and sorts if you'd like
Now supports setting an autoincrementing ID for your NSManagedObject
subclasses
This 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.
Object relationships are also generated from dictionaries, but disabled by default. Set SwiftRecord.generateRelationships
to true to enable this feature. See the relationships section
Visit ark for a more beginner friendly guide to SwiftRecord
Check out our UIClosures library too
if you love SwiftRecord, tweet it!
- Installation
- Creates, Saves, Deletes
- querying
- [sorting, limits](#sorting, limits)
- aggregation
- [custom coredata](#custom core data)
- mapping
- relationships
- autoincrement
via CocoaPods
-
Edit your Podfile to use frameworks and add SwiftRecord:
platform :ios, '8.0' use_frameworks! pod 'SwiftRecord'
-
run
pod install
-
Just add SwiftRecord to your Cartfile:
github "arkverse/SwiftRecord" >= 0.0.1
-
and run
carthage update
Drag and drop either Classes/SwiftRecord.swift
or SwiftRecord.framework
into your project
-
At the top of every
NSManagedObject
swift class add:import SwiftRecord class Event: NSManagedObject { @NSManaged var eventID: NSNumber @NSManaged var name: String @NSManaged var type: String @NSManaged var when: NSDate @NSManaged var creator: User @NSManaged var attendess: NSSet }
-
Set up Core Data by naming your model file
MyAppName.xcdatamodel
. Optionally, you can set your own model name by callingCoreDataManager.sharedManager.modelName = "mymodelname"
var event = Event.create() as! Event // Downcasts are necessary,
event.type = "Birthday"
event.when = NSDate()
event.save() // simple save
event.delete() // simple delete
var properties: [String:AnyObject] = ["name":"productQA","type":"meeting", "when":NSDate()]
var meeting = Event.create(properties) as! Event // Remember to downcast
Note: 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
*instancetype
, Self
in Swift, has only just been added, and still pretty useless
Easily query against your entities. Queries accept optional condition, sort and limit parameters.
// grab all Events
var events = Event.all() as! [Event]
// all past events before now
var pastEvents = Event.query("when < %@", NSDate()) as! [Event]
// specific event, yes we have support for format&arguments. Note, finding specific events return optional vars
var thisEvent = Event.find("name == %@ AND when == %@", "productQA", NSDate()) as? Event
// Use dictionaries to query too
var birthdayEvents = Event.query(["type":"birthday"]) as! [Event]
// or NSPredicates
var predicate = NSPredicate("type == %@", "meeting")
var meetingEvents = Event.query(predicate) as! [Event]
// Events sorted by date, defaults to ascending
var events = Event.all(sort: "when") as! [Event]
// Descending
var descendingEvents = Event.all(sort:["when":"DESC"]) as! [Event]
// or
var descEvents = Event.all(sort:"when DESC, eventID ASC")
// All meeting events sorted by when desc and eventID ascending and limit 10
var theseEvents = Event.query(["type":"meeting"], sort:["when":"DESC","eventID":"ASC"], limit: 10) as! [Event]
// NSSortDescriptor as sort arg (or array of NSSortDescriptors
Event.all(sort: NSSortDescriptor(key:"when",ascending:true))
// count all Events
var count = Event.count()
// count meeting Events
var count = Event.count("type == 'meeting'")
if you made your own, feel free to set it
var myContext: NSManagedObjectContext = ...
CoreDataManager.sharedManager.managedObjectContext = myContext
Don't set these if you set your own context. If you have a modelName different from the name of your app, set it. If you want a different databse name, set it.
CoreDataManager.sharedManager.modelName = "MyModelName"
CoreDataManager.sharedManager.databaseName = "custom_database_name"
The most of the time, your JSON web service returns keys like first_name
, last_name
, etc.
Your Swift implementation has camelCased properties - firstName
, lastName
.
We automatically check against camelCase variations.
If you have different variations, override static func mappings() -> [String:String]
to specify your local to remote key mapping
The key string is your local name, the value string, your remote name.
// this method is called once, so you don't have to do any caching / singletons
class Event: NSManagedObject {
override class func mappings() -> [String:String] {
return ["localName":"remoteName","eventID":"_id","attendees":"people"]
}
// firstName => first_name is automatically handled
}
@end
While 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:
SwiftRecord.generateRelationships = true
Once this is set, you rels will be filled, ie:
var personDict = ["email":"zaid@arkverse.com","firstName":"zaid"]
var eventDict = ["name":"product QA meeting","when":NSDate(),"creator":personDict]
var event = Event.create(eventDict) as! Event
var person: Person = event.creator
println(person.username)
If 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:
public class Event: NSManagedObject {
override public func autoIncrementingId() -> String? {
return "eventId"
}
Note, 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.
ObjectiveRecord supports CoreData's in-memory store. In any place, before your tests start running, it's enough to call
CoreDataManager.sharedManager.useInMemoryStore()
- improve Swiftiness
- JSON generation
- Possible add NSOrderedSet support in Relationship init
- Support Realm
- Better typing
SwiftRecord is available under the MIT license. See the LICENSE file for more information.
Check out ark for more about us