Code Monkey home page Code Monkey logo

storedmodel's Introduction

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

storedmodel's People

Contributors

siannopollo avatar aopsfan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.