Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A friendlier interface for using CoreData in Cocoa and iOS applications
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
StoredModel.xcdatamodel
StoredModel.xcodeproj
build
.gitignore
Dog.h
Dog.m
DogTest.h
DogTest.m
LICENSE
README.textile
StoredModel.h
StoredModel.m
StoredModelTest.h
StoredModelTest.m
StoredModel_Prefix.pch
Tests-Info.plist

README.textile

StoredModel

A friendlier interface for using CoreData in Cocoa and iOS applications. Borrows mostly from easy-to-use syntax in the Ruby ActiveRecord library.

Why?

Because I looked at all the example code for using CoreData and thought there had to be a better way of saving data than passing a NSManagedObjectContext around my whole application. Turns out, there is! This library greatly reduces the verbosity of working with NSManagedObjects, and should be pretty familiar to anyone who has used Ruby on Rails.

Installation

This can be done a couple of ways. The first is really simple, and just requires that you pull the StoredModel.h/m files into your project (git clone git://github.com/siannopollo/StoredModel.git && cp StoredModel/StoredModel.* /path/to/your/project).

The other way is to link against the static library. This is a multipart process, so just bear with me:

  1. cd into your project directory
  2. run git clone git://github.com/siannopollo/StoredModel.git
  3. In Xcode, drag the StoredModel.xcodeproj file into the root of your project to link to the StoredModel project. You will now have a libStoredModel.a library for your linking pleasure.
  4. Right click on your target(s) and click Get Info. In the General tab, click “+” under Direct Depenedencies and add the StoredModel dependency and click “Add Target”. Then click “+” under Linked Libraries and click “Add Other…”. Navigate to your/project/directory/StoredModel/build/Release-iphonesimulator, choose libStoredModel.a and click “Add”.
  5. Now that you are linked to the StoredModel library, you still need to tell Xcode where to find the StoredModel.h file. Right click on your target(s) again and click Get Info. Under the “Build” tab type “header search” in the search box. Double click on the field that says Header Search Paths. Add the following as a new path: ${PROJECT_DIR}/StoredModel/build/${BUILD_STYLE}-${PLATFORM_NAME}/usr/local/include.

If you got lost at any point during that, have a look at this more visual tutorial.

You should now be able to #import "StoredModel.h" into any of your project files and use the StoredModel class.

Usage

Most CoreData example code has a NSManagedObjectContext created in the application delegate, and that is then passed to a root view controller so that thing can pass the context around like a joint at a Snoop Dogg concert. Instead of that, you need only deal with the managedObjectContext once. Your code in your application delegate should look something like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  ... // controller setup
  
  [StoredModel setContext:self.managedObjectContext];
  
  ... // keying up the window
}

Once StoredModel has a reference to the managedObjectContext, all subclasses of StoredModel will have access to that same context. So let’s say you have a Dog class (not to be confused with the Dogg class).

To instantiate a new Dog object => Dog *dog = [Dog new]
To save a dog => [dog save]
To find a dog by name => Dog *dog = [Dog findFirst:@"name = 'Spot'"]
To find all dogs by name => NSMutableArray *dogs = [Dog find:@"name = 'Spot'"]
To count all dogs => [Dog count]
To destroy a dog => [dog destroy]

You can look at the tests for other use cases.

Contribution

Pull requests are welcomed and encouraged.

License

See the LICENSE.

Todo

  • See about supporting multiple NSManagedObjectContexts
Something went wrong with that request. Please try again.