A friendly abstraction for CoreData
Switch branches/tags
Nothing to show
Pull request Compare This branch is 3 commits behind thinktopdown:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



A friendly abstraction for CoreData

Aim: Gain all the speed of CoreData but with a friendly, ActiveRecord style interface.

Step 1: Install SimpleData

  • Add the SimpleData repository to your project
  • Add all the .m files in Lib to your target
    • Right click the .m file
    • In the target tab, check the box for your target

Step 2: Create a data model

  • Go to File > New File then choosing Resource > Data Model
  • Open the data model and edit it to your hearts desire
  • Tip: you might want to create date attributes called createdAt and updatedAt as they will come on handy later

Step 3: Create the class representations

  • In the data model editor, select classes you wish to use in your project
  • Choose File > New File. In the New File dialog, select Managed Object Class.
  • Depending on the version of Xcode you’re using, the Managed Object Class may be available in the iPhone OS section under Cocoa Touch Classes, or you may need to choose the template in the Mac OS X section, under Cocoa—either will work correctly.
  • Click Next. The correct location and targets should have been selected for you. Click Next to accept them.
  • You should see the Entity selection pane, with the Event entity selected. The “Generate accessors” and “Generate Objective-C 2.0 properties” options should also be selected.
  • Click Finish to generate the files.
  • In the header files for the generated classes, replace “: NSManagedObject” with “: SimpleModel”

You are now ready to start working with SimpleData

Step 4: Create a SimpleStore

  • First, import the SimpleStore header
    #import <SimpleStore.h>
  • To create or open an existing database simply call:
    [SimpleStore storeWithPath: @"path"];

Step 5: Enjoy!

  • To create an object:
    [User createWithAttributes:attributesDict];
    [User createWithName:@"Brian" email:@"bricollins@gmail.com"];
    [User createWithEmail:@"john@example.com" name:@"John"];
  • To find an object:
    [User findByName:@"Brian"];
    [User findAll]
    NSArray *users = [User findAllByStarSign:@"Libra" sortBy:@"name" sortByDescending:@"createdAt"];
  • To update an object:
    User *u = [User findByName:@"Brian"];
    u.name = @"Brian Collins";
    [u save];
  • Setting non object attributes (unfortunately):
    [User createWithName:@"Brian" 
     smoker:[NSNumber numberWithBool:NO]];
  • Find or create an object if it doesn’t exist:
    [User findOrCreateByName:@"Brian" email:@"brian@example.com"].email; // => @"brian@example.com"
    [User findOrCreateByName:@"Brian" email:@"brian@foo.com"].email; // => @"brian@example.com" because it exists

Running the test suite

  • Build and run with the target “Test” and watch the log output

Displaying SimpleData objects in a tableView

  • Subclass SDTableViewController


  • Currently,
    • You may only have one SimpleStore open per thread.
    • There is no validation, but the standard coredata validation should still work.
    • You will get warnings unless you prototype the methods you use. See this wiki article

See Also