Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
ActiveRecord for Objective-C
Objective-C Ruby

This branch is 3 commits ahead, 152 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
Classes
Example
.travis.yml
LICENSE
ObjectiveRecord.podspec
README.md

README.md

Intro

This is a lightweight ActiveRecord way of managing Core Data objects. The syntax is borrowed from Ruby on Rails.
And yeah, no AppDelegate code. It's fully tested with Kiwi.

Build Status

Usage

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

Create / Save / Delete

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

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

Finders

// 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 where:@"name == 'John' AND surname == 'Doe'"].first;

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

// I wanna be fancy and write my own NSPredicate
[NSPredicate  predicateWithBlock:^BOOL(Person *person, NSDictionary *bindings) {
    return person.isMember == YES;
}];
NSArray *members = [Person where:membersPredicate];

Custom ManagedObjectContext

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

Person *john = [Person createInContext:newContext];
Person *john = [Person where:@"name == 'John'" inContext:newContext].first;
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 instance].modelName = @"MyModelName";
[CoreDataManager instance].databaseName = @"custom_database_name";

Examples

// 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.name = @"John";
john.surname = @"Wayne";
[john save];

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

  [person delete];
}];
Something went wrong with that request. Please try again.