Skip to content
This repository

A lightweight Active Record - style of managing CoreData objects.

branch: master

Remove/ignore Podfile.lock

While the lockfile is important for private apps, it gets in the way for
open source libraries. Bundler, for example, encourages gem builders to
_not_ check in the Gemfile.lock. We should adopt a similar practice.

Signed-off-by: Stephen Celis <>
latest commit b4d282dba0
Stephen Celis stephencelis authored
Octocat-spinner-32 Classes Support descending/multiple string ordering March 27, 2014
Octocat-spinner-32 Example Remove/ignore Podfile.lock April 05, 2014
Octocat-spinner-32 .gitignore Remove/ignore Podfile.lock April 05, 2014
Octocat-spinner-32 .travis.yml update Makefile December 10, 2013
Octocat-spinner-32 update changelog April 03, 2014
Octocat-spinner-32 LICENSE s/ March 20, 2014
Octocat-spinner-32 ObjectiveRecord.podspec Update podspec for 1.5.0 April 04, 2014
Octocat-spinner-32 README updates April 04, 2014

ObjectiveRecord CocoaPod Build Status

Objective Record is a lightweight ActiveRecord way of managing Core Data objects. If you've used Ruby on Rails before, it might sound familiar.

No AppDelegate code required. It's fully tested with Kiwi.


  1. Install with CocoaPods or clone
  2. #import "ObjectiveRecord.h" in your model or .pch file.

Create / Save / Delete

Person *john = [Person create]; = @"John";
[john save];
[john delete];

[Person create:@{ 
    @"name" : @"John",
    @"age" : @12, 
    @"member" : @NO 


// all Person entities from the database
NSArray *people = [Person all];

// Person entities with name John
NSArray *johns = [Person where:@"name == 'John'"];

// And of course, John Doe!
Person *johnDoe = [Person find:@"name == %@ AND surname == %@", @"John", @"Doe"];

// Members over 18 from NY
NSArray *people = [Person where:@{ 
                      @"age" : @18,
                      @"member" : @YES,
                      @"state" : @"NY"

// You can even write your own NSPredicate
NSPredicate *membersPredicate = [NSPredicate  predicateWithBlock:^BOOL(Person *person, NSDictionary *bindings) {
    return person.isMember == YES;
NSArray *members = [Person where:membersPredicate];

Order and Limit

// People by their last name ascending
NSArray *sortedPeople = [Person allWithOrder:@"surname"];

// People named John by their last name Z to A
NSArray *reversedPeople = [Person where:@{@"name" : @"John"} 
                                  order:@{@"surname" : @"DESC"}];

// You can use NSSortDescriptor too
NSArray *people = [Person allWithOrder:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];

// And multiple orderings with any of the above
NSArray *morePeople = [Person allWithOrder:@"surname ASC, name DESC"];

// Just the first 5 people named John sorted by last name
NSArray *fivePeople = [Person where:@"name == 'John'"
                              order:@{@"surname" : @"ASC"}


// count all Person entities
NSUInteger personCount = [Person count];

// count people named John
NSUInteger johnCount = [Person countWhere:@"name == 'John'"];

Custom ManagedObjectContext

NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
newContext.persistentStoreCoordinator = [[CoreDataManager instance] persistentStoreCoordinator];

Person *john = [Person createInContext:newContext];
Person *john = [Person find:@"name == 'John'" inContext:newContext];
NSArray *people = [Person allInContext:newContext];

Custom CoreData model or .sqlite database

If you've added the Core Data manually, you can change the custom model and database name on CoreDataManager

[CoreDataManager sharedManager].modelName = @"MyModelName";
[CoreDataManager sharedManager].databaseName = @"custom_database_name";


// find
[[Person all] each:^(Person *person) {
    person.member = @NO;

for(Person *person in [Person all]) {
    person.member = @YES;

// create / save
Person *john = [Person create]; = @"John";
john.surname = @"Wayne";
[john save];

// find / delete
[[Person where: @{ @"member" : @NO }] each:^(Person *person) {
    [person delete];


The most of the time, your JSON web service returns keys like first_name, last_name, etc.
Your ObjC implementation has camelCased properties - firstName, lastName.

Since v1.2, camel case is supported automatically - you don't have to do anything! Otherwise, if you have more complex mapping, here's how you do it:

// just override +mappings in your NSManagedObject subclass
// this method is called just once, so you don't have to do any caching / singletons
@implementation Person

+ (NSDictionary *)mappings {
  return @{ 
      @"id": @"remoteID",
      @"mmbr": @"isMember",
      // you can also map relationships, and initialize your graph from a single line
      @"employees": @{
          @"class": [Person class]
      @"cars": @{
          @"key": @"vehicles",
          @"class": [Vehicle class]
  // first_name => firstName is automatically handled



ObjectiveRecord supports CoreData's in-memory store. In any place, before your tests start running, it's enough to call

[[CoreDataManager sharedManager] useInMemoryStore];


  • NSIncrementalStore support


ObjectiveRecord is available under the MIT license. See the LICENSE file for more information.

Something went wrong with that request. Please try again.