Tuesday, April 10, 2012

Using core data with an existing single view application

I'm trying to learn about Core Data and am following the "Locations" tutorial from Apple. One immediate stumbling block i've come across is that I have already started constructing the application that i want to use Core Data with. It is a single view application.



What are the steps I need to take in order to utilize Core Data with this application? The tutorial says to select the checkbox "use core data for storage" when creating the project but there must be a way to enable core data after the project creation.



I'd really appreciate some help. Thanks.





3 comments:

  1. Go to Targets -> Build Phases -> Link Binary with libraries and add CoreData.framework. You should be good to go...

    ReplyDelete
  2. Of course you can add core data support after you already started or created you app.

    I would recommend you to do the following:

    Add core data framework to your target
    Add a object model "Add new File -> Core Data -> Data Model"
    Create your initial object model
    Create NSManagedObject Subclasses for all your models in your object model
    Create a Controller (subclass NSObject) where you do a singleton / static instance (singleton)
    In your controller add some data-layer method like "getObjectsXYZ", "saveData", etc.

    In your existing app you can then load objects through your controller class like "[[mycontroller sharedInstance] getObjectYByName:@"some text"]"

    Of course you need to dig into core data. :)

    If you used to ORM's then you might look at: mogenerator vim rentsch. http://rentzsch.github.com/mogenerator/

    This makes data models very very easy. I swear on that tool!

    ReplyDelete
  3. Honestly, if you only have a single view it's probably easiest just to create the application from scratch as the tutorial says. There are quite a few bits and pieces that you need to include and it could save you a lot of time in the long run.

    However, you can do it by hand:

    Add the Core Data framework to your project
    Add a data model
    Add code in your app delegate to create the managed object context (including all the error conditions)

    ReplyDelete