Get $500+ of the best After Effects files, video templates and music for only $20!
iPhone Core Data: Your First Steps

iPhone Core Data: Your First Steps

Tutorial Details
  • Technology: iPhone SDK
  • Difficulty: Intermediate
  • Estimated completion time: 45 minutes

Core Data is a framework Apple provides to developers that is described as a “schema-driven object graph management and persistence framework.” What does that actually mean? The framework manages where data is stored, how it is stored, data caching, and memory management. It was ported to the iPhone from Mac OS X with the 3.0 iPhone SDK release.

In this tutorial I am going to guide you through the process of creating a project with Core Data and show you how to use it with a simple UITableViewController. It will be a simple one table database that will store Lap Times and display the times and their split differences in a UITableView.

Why use Core Data?

Before we get started, it is important to understand why you might want to use Core Data with SQLite for storage over property lists, a custom XML format, or direct SQLite database access. The Core Data API allows developers to create and use a relational database, perform record validation, and perform queries using SQL-less conditions. It essentially allows you to interact with SQLite in Objective-C and not have to worry about connections or managing the database schema, and most of these features are familiar to people who have used object-relational mapping (ORM) technologies like those implemented in Ruby on Rails, CakePHP, LINQ or other libraries and frameworks that abstract access to the database. The main benefit to this approach is that it eliminates the development time otherwise necessary to write complex SQL queries and manually handle the SQL and output of those operations.

Creating a New iPhone project with Core Data

The example application we will be building today is a simple lap timer. It will create a new record in the Core Data store for each lap we make. The UITableView will then show the difference between the current and last laps.

core data lap timer final preview

To get started we will open XCode and create a new project. Name it what you wish, I have named it “LapTimer”. We will be creating a “Window-based application” from the New Project template selection. Make sure “Use Core Data for storage” is checked.

new core data project screen

The project should be familiar to what you may have seen before if you have developed iPhone applications previously.

Setting up The Core Data Structure

There is not a lot of setting up to do as the “Use Core Data for storage” option in the “Window-based Application” template has automatically set some important variables and created files in the project for us.

The LapTimer.xcdatamodel file is where we will define the schema for our SQLite database. The Core Data framework was also added to the project to include the files for API access. The other changes are made within the default application files. Notably, the application delegate files have the methods to setup the Core Data store in our application and to reference it in child UIViewControllers.

What we are interested in at the moment is the LapTimer.xcdatamodel file under “Resources.” This file gives you a visual map of your schema showing entities and attributes.

There are some different terms and phrases used in Core Data that can be loosely related to regular database names, but they are not identical.

An “entity”, also known as a “managed object”, is similar to a table. It is a the definition of an object that will contain a collection of data. An entity object contains “attributes.” These can be loosely associate with columns, but these attributes are not just limited to data storage. Attributes can define a relationship between two entities, a dynamically fetched property, or define a property for data storage.

iphone core data diagram

From the diagram above, you can get a feel for how flexible objects derived from Core Data entities are. For this example application we will need a really simple entity. We will call the entity “Event” which will store records of our laps.

iphone core data model

What we will do to create this entity is click the [+] button in the first (from the left) upper column. This will create a new entity in the list and visually on the schema map below the columns.

iphone core data managed object

This is a nice visual editor for your model. Core Data really does the heavy lifting when it comes to the “M” (Model) part of MVC. The visual editor shows the relationships, properties, and the entities of the store while the schema is dynamically created and managed all for you. This is similar to Interface Builder as it takes care of allocating, managing and placing objects for you on a UIView without a single line of code.

With the new Event entity in place, we want to create a “property.” Since this property will be storing data we will set it as an “attribute.” So this new attribute will just store the current date for when the record was created. In our example application we will use this to reference our lap times.

The next column to the right is where we define our properties (make sure the Event entity is selected). So, create a new property using the [+] button in the column, select “Add Attribute”.

iphone core data attributes

We will call the attribute “timeStamp” and set its Type to “Date”. In the type select drop-down there are similar column data types to those available in relational database systems like PostgreSQL or MySQL, including data types like integers, floats, strings, booleans, dates, and binary data (blobs).

For this attribute, we don’t need any other options selected or changed.

That is it for the xcdatamodel file, and we can move onto integrating it into our app. Now is a good time to save your work.

Creating our model files

If you have used an MVC framework that has database model definitions defining a table’s structure or behaviors, then this will be a familiar task.

We need to start by creating a definition of the entity. We do this by creating a NSManagedObject class of the entity and define the variables the store has.

This is a simple process. Select the Event entity in the LapTimer.xcdatamodel file and go File > New File. You will see that there is a new file template in the “Cocoa Touch Class” section called “Managed Object Class”.

new managed object class

Select the template and hit “Next.” The next screen is just defining where we save the file and the target to include it with, this is all correct by default so hit “Next” again. The next screen is where you define what entities you want to create NSManagedObject classes for. If it isn’t selected, select Event and make sure the “Generate Accessors” and “Generate Obj-C 2.0 properties” checkboxes are selected, we will not need validations at this time. Hit Finish.

managed object class generation

So now we have 2 new files in our application. Event.m and Event.h. They define the NSManagedObject class for the Event entity we created in our Core Data store. They define the timeStamp field so when we want to use the Event class we can access the attribute.

Event.h

#import <CoreData/CoreData.h> 

@interface Event :  NSManagedObject  
{ 

} 

@property (nonatomic, retain) NSDate * timeStamp; 

@end

Event.m

#import "Event.h" 

@implementation Event  

@dynamic timeStamp; 

@end

Like model definitions in other frameworks and languages you can add custom methods for all records in the Event entity. You will notice the timeStamp attribute has been added and assigned as an NSDate object.

Core Data and KVC

With Core Data setup it is now time to work on some of the controller logic in the application. We will use a UITableViewController as the main interface of the app to show the lap times as well as logging a new time.

So, we will create the new UITableViewController with File > New File. Then under the iPhone section select “UIViewController subclass” and check “UITableViewController subclass” but not checking boxes that relate to using XIB’s or for targeting for an iPad. We will not be using a XIB for this controller. Call the new file “TimeTableController” and finish the file wizard.

In this controller we will need 2 properties, a reference to the NSManagedObjectContext and an array to store the records in for the UITableView. As well as defining those properties, we will import the Event.h file so we can use the class.

TimeTableController.h


#import <UIKit/UIKit.h> 

#import "Event.h" 

@interface TimeTableController : UITableViewController { 

    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *eventArray; 

} 

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *eventArray; 

- (void) fetchRecords;
- (void) addTime:(id)sender; 

@end

What is an NSManagedObjectContext? It’s referred to as the “scratch pad” for Core Data in the application for managing fetching, updating, and creating records in the store. It also manages a few fundamental features in Core Data including validations and undo/redo management for the records.

The managed object context is the connection between your code and the data store. All of the operations you will execute for Core Data do so against the managed object context. When a request is performed, the managed object context will then talk to the persistent store coordinator which is responsible for mapping the objects to data for the data store. This allows the Core Data to be flexible between different data store formats. Here’s a diagram of how this looks.

iphone core data diagram 2

With the header file defined we need to propagate the allocated properties and methods in the implementation file.

TimeTableController.m


#import "TimeTableController.h"
@implementation TimeTableController
@synthesize managedObjectContext, eventArray; 

// ...
// ... default commented code from the file template
// ... 

- (void)viewDidLoad { 

    [super viewDidLoad];
    self.title = @"Lap Times";
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTime:)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
    [self fetchRecords]; 

} 

- (void)addTime:(id)sender { 

    Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
    [event setTimeStamp: [NSDate date]]; 

    NSError *error;

    if(![managedObjectContext save:&error]){

	    //This is a serious error saying the record
	    //could not be saved. Advise the user to
	    //try again or restart the application. 

    }

    [eventArray insertObject:event atIndex:0];

    [self.tableView reloadData];

}

- (void)fetchRecords { 

    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]; 

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity]; 

    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release]; 

    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 

    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    } 

    // Save our fetched data to an array
    [self setEventArray: mutableFetchResults];
    [mutableFetchResults release];
    [request release];
} 

// ...
// ... more template comments and default method definitions
// ... 

- (void)dealloc {
    [managedObjectContext release];
    [eventArray release];
    [super dealloc];
} 

@end 

Thats a fair bit of code so lets go through it by each method individually. There is code from when you create the file from the template, commented out methods like viewDidUnload, which I have just left out from the above.

viewDidLoad

We start off with the usual call to the super class. Then we define the UINavigationBar’s title. After that we need to define the button we will use to add a record to the Core Data store. When the button is pressed we tell it to call the selector addTime: which will then interact with Core Data. After the required releases we get to calling the fetchRecords function which is explained below.

addTime:(id)sender

This is fired when the UIBarButtonItem in the upper right of the UINavigationBar is pressed. We need to create a new Event record with the current NSDate and save it to the database.

We create a new Event record which is called an NSEntityDescription. This is your row in the database for the new record. To do this we define the entity name the record belongs to and provide the NSManagedObjectContext. The current date is then set against the timeStamp attribute.

The save operation is then performed, but there is a provision to handle an error if the insert fails. You would usually throw a UIAlertView saying the record did not create, and perhaps instruct the user to try again or close and reopen the application.

The record needs to be added to the array that the UITableView feeds from. Then the UITableView needs to be told to reload the data. You can do this more graphically with an animation but for the sake of the tutorial, let’s keep it simple.

fetchData

This method will get the data from the store and add it to the array we have in the controller. For further detail on getting records let’s have a close look at NSFetchRequest.

Fetching data from the data store

Core Data has a different approach to fetching data from its database. It is a NoSQL data store, meaning all the conditions of a query are based upon methods. This is great as the base store, which is SQLite, could be changed to any other database technology and the only thing to change would be the connection and drivers for the database.

So, to create a request we create an NSFetchRequest object. This is the base object that the query conditions will be set against. We can define conditions for matching a particular property based on how records are ordered. You can find out more about Core Data and its conditions for NSFetchRequests in their documentation.

Creating an new NSFetch request is simple. You just need to define the entity you want records from and the NSManagedObjectContext.


NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setEntity:entity];

The entity is defined using an NSEntityDescription object which requires the entity name and the NSManagedObjectContext. The fetch request is then created passing the entity description. This would equate to the first part of a SQL statement:


SELECT * FROM `events`

In our example application we sort the data by the timeStamp in a descending manner. To do this, we use a NSSortDescriptor.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; 

NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 

[request setSortDescriptors:sortDescriptors]; 

[sortDescriptor release];

The NSSortDescriptor is created and we define the attribute we wish to sort and wether it is ascending, in this case we want it to descend so it is set to NO. The fetch request can take many sort descriptors so it accepts an array when setting the sort descriptors. Since we only want one, we just need to create an array with one object in it. We set the descriptor array against the fetch request and that is that.

For defining a condition to match a records contents the NSPredicate class comes into play. It allows the fetch request match or define a range that the contents of a record must meet. This is the equivalent to your equals, greater and less than matches in SQL. It does have more than your basic SQL functions, which you can see here.

Setting a predicate can be very simple.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lastName like %@) AND (birthday > %@)", lastNameSearchString, birthdaySearchDate];

Using NSPredicate predicateWithFormat: is a simple and familiar method that allows you to define the conditions of the query. For an in depth explanation on NSPredicates the Apple Documentation has some great guides.

When you have defined the conditions in your fetch request you can then execute it.

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

That will return an array of entity objects, NSManagedObjects, to use in your data output.

Populating the UITableView from Core Data

With the data fetched from Core Data and stored in the eventArray we can now output those records into the UITableView.

First thing is to tell the table that we will only require 1 section and how many rows we have to use.

Excerpt From TimeTableController.m


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [eventArray count];
} 

If you have used a UITableViewController before, the next function should be straight forward.


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell";
static NSDateFormatter *dateFormatter = nil; 

if (dateFormatter == nil) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"h:mm.ss a"];
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
} 

Event *event = [eventArray objectAtIndex: [indexPath row]];
Event *previousEvent = nil; 

if ([eventArray count] > ([indexPath row] + 1)) {
    previousEvent = [eventArray objectAtIndex: ([indexPath row] + 1)];
}
	

[cell.textLabel setText: [dateFormatter stringFromDate: [event timeStamp]]]; 

if (previousEvent) {
    NSTimeInterval timeDifference = [[event timeStamp] timeIntervalSinceDate: [previousEvent timeStamp]];
    [cell.detailTextLabel setText: [NSString stringWithFormat:@"+%.02f sec", timeDifference]];
} else {
    [cell.detailTextLabel setText: @"---"];
} 

return cell; 

}

The cell will display 2 values, from using the UITableViewCellStyleValue1 style. The left will be the time of the lap and the right will be the difference in seconds from the previous record.

Since this method iterates we need to take special care with the load it can put the device under if it isn’t managed correctly. For that reason the NSDatFormatter is stored as a static variable so it can be reused in each iteration without allocating and releasing it every time.

Lazy Loading

Lazy Loading is a technique where you delay the request or allocation of a property as much as possible. This helps keep the memory down and during iterative functions this is paramount. Knowing when and how to allocate data is crucial to keeping a mobile app speedy. Deallocating the object is also as important, the earlier the better.

cellForRowAtIndexPath: is an iterative method and any data processed or allocated in this method especially needs to be kept at a minimum. This method is run whenever a cell comes into view so when a user is scrolling quickly this particular method, depending on the record set size, can be called very frequently in succession.

The next task is to get the event object associated with the table row that needs to be rendered. Since we need to get the previous record for the time comparison there is a simple check to see if there is a previous record and to store it into previousEvent. If the previousEvent exists then we calculate the split using [NSDate timeIntervalSinceDate: (NSDate)]. The textLabel and detailedTextLabel are then set with the values we have calculated.

Finishing the application

With the UITableViewController setup and the table data source working with the Core Data store all that is needed is to load the controller when the application is started.

In the Application controller a UINavigationController property needs to be defined. Then the applicationDidFinishLaunching method just needs to allocate the controller and we are done.

LapTimerAppDelegate.h


@interface LapTimerAppDelegate : NSObject <UIApplicationDelegate> {
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;    
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
    UIWindow *window;
    UINavigationController *navigationController;
} 

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController; 

- (NSString *)applicationDocumentsDirectory; 

@end 

Excerpt from LapTimerAppDelegate.m


#import "LapTimerAppDelegate.h"
#import "TimeTableController.h" 

@implementation LapTimerAppDelegate
@synthesize window, navigationController; 

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    TimeTableController *tableController = [[TimeTableController alloc] initWithStyle:UITableViewStylePlain]; tableController.managedObjectContext = [self managedObjectContext];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
    [tableController release];
    [window addSubview: [self.navigationController view]];
    [window makeKeyAndVisible];
}

// ...
// ... other template methods
// ... 

- (void)dealloc {
    [managedObjectContext release];
    [managedObjectModel release];
    [persistentStoreCoordinator release];
    [window release];
    [navigationController release];
    [super dealloc];
}

The TimeTableController.h file is included into the application delegate and then allocated with a UINavigationController.

That should be it. Build the application to check for errors. Some of the code examples have only been extracts, none of the code that is generated when creating a file has been removed, only filled in. If you run into errors you can’t crack, you can download the project file attached to this tutorial which you can then compile and compare.

Run the application. You will see the navigation controller and the add button. Press the add button and you will get a new time in the table.

Recap

In this tutorial we have created an example application to store simple data into a Core Data store. The application ran through the initial setup procedure when creating an application with Core Data, defining the data structure and fetching records from the data store.

Hopefully you have gotten an introduction to Core Data and can see how easy it is to use and how it can improve your applications performance and functionality.

If you want to find out more about Core Data or want a detailed look at the framework’s structure then the Apple Developer Documentation is the perfect place to go.

Resources

Apple Developer Documentation:

Introduction to Core Data Programming

Core Data Migration and Versioning

NSPredicate Programming Guide:

Add Comment

Discussion 72 Comments

Comment Page 1 of 21 2
  1. Great article! Thank you :)

  2. Karlsson says:

    Fantastic Article!

  3. morbec says:

    Amazing !!! Thank you very much !!!

  4. hao says:

    Really great tutorial

  5. Sid says:

    Awesome Tut.. :) Thnx

  6. Great Article .. love to code Iphone Apps ..

  7. HB says:

    Thank you for the great tutorial,
    it helps me a lot so far, but there is one thing I don’t understand:

    In the TimeTableController.h the ivar managedObjectContext + property is created, but it’s never allocated and initialized, so how can a call of the property return an instance of it?

    • Author

      HB,

      The managedObjectContext properties in UIViewControllers are usually allocated from the Application Delegate. If you have a look at “Excerpt from LapTimerAppDelegate.m” it allocates timeController.managedObjectContext with [self managedObjectContext]. The code preview above may show it a little funny, no line break with the line before it.

      Calling [self managedObjectContext] in the App Delegate will return the NSManagedObjectContext for the application. The method was created with the application template when we selected “Use Core Data for storage”.

  8. HB says:

    The line is cut off at the end, so I didn’t see that the property gets the managedObjectContext from the Delegate.

    Now it’s clear :-)

    Thank you Alex

  9. Ben says:

    I don’t want to sound out of place, but you cut off the right portion of your sample code a lot, and it is confusing me.

  10. raj says:

    What am I missing here? This is an interesting article on Core Data, but after carefully typing in all the code, it showed a blank screen. So I downloaded the source code ZIP file, extracted, then built and ran that. 5 errors. How about source that works? Part of the problem is that a number of the class files are in the wrong directory, but even after manually fixing the ZIP source, there's still 2 "exit errors" that I know nothing about.

    Any thoughts? It'd be nice to have an updated version of the source that works as is.

    thanks

    • Author

      Hi Raj,
      I have had reports that the source code may not be working with the new XCode and SDK. It was made a month ago with 3.1.3. I will update the source for 4.0 and alter any parts of the article for 4.0
      Cheers.

    • Pooria says:

      This is how your LapTimerAppDelegate.h should look like:

      #import
      #import “TimeTableController.h”

      @interface LapTImerAppDelegate : NSObject {
      }

      @property (nonatomic, retain) IBOutlet UIWindow *window;

      @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
      @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
      @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

      - (void)saveContext;
      - (NSURL *)applicationDocumentsDirectory;

      @end

      And this is how your application:didFinishLaunchingWithOptions: method should look like:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      TimeTableController* tableController = [[TimeTableController alloc] initWithStyle:UITableViewStylePlain];
      tableController.managedObjectContext = [self managedObjectContext];

      UINavigationController* navController = [[UINavigationController alloc] init];
      [navController pushViewController:tableController animated:YES];
      [self.window addSubview:navController.view];

      [tableController release];
      [self.window makeKeyAndVisible];
      return YES;
      }

  11. raj says:

    While I had temporarily installed the 4.0 SDK/ 3.2.3 Xcode, I reverted back to 3.2.2 Xcode before trying this article's source code. The 2 exit errors are still there. Both are the same error msg, but for TimeTableController.m and Event.m:

    Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

    Might have something to do with my switching Xcode & SDK versions, though other code I'm working on doesn't seem to be affected. There are no line numbers in the error msgs.

  12. Author

    The source files download has been updated and tested with the iOS 4 SDK.

    I will be revising the tutorial to make a few minor adjustments for the new SDK.

  13. Brad says:

    Hi Alex,

    Great tutorial! I created a new project based on your tutorial step by step. I am getting the following error message

    ‘+entityForName: could not locate an NSManagedObjectModel for entity name ‘Event”

    I can’t for the life of me figure out why it can’t locate the managedModelObject.

    Any suggestions would be great!

    • Author

      Hmm. Sounds like you might not be including Event.h to the view. Either that or the data store want setup with the Event entity. Just review that Event.h is included in the view you are using and that the data store shows the event entity.

      If you still have no luck let me know, can arrange a chat session to help you out.

      Cheers.

      • Abhinav says:

        i guess it was because the managed object for the table controller is not set up. I too got this error as i finished this article. As this is a problem faced by many persons inspite of a nice tut, here is what the method looks like for me

        - (void)applicationDidFinishLaunching:(UIApplication *)application {

        // Override point for customization after app launch
        // Override #1
        TimeTableController * ttController = [[TimeTableController alloc] initWithStyle:UITableViewStylePlain];
        ttController.managedObjectContext = self.managedObjectContext;
        navController = [[UINavigationController alloc] initWithRootViewController:ttController];
        [window addSubview:navController.view];
        [ttController release];
        [window makeKeyAndVisible];
        }

  14. Durgadas says:

    Awesome Tutorial…Got to know more about core data.

  15. manju says:

    Hi Alex,

    I following the same procedure but i got the white blank screen in i phone simulator please help me out in this.

    thanks and regards

    Manju .K.S

    • Author

      If its blank it sounds like the view controller wasn’t attached to the window. Check over the Application Delegate and especially where it set’s the navigation controller. See the source code, LapTimerApplicationDelegate.m and line 25.

      Also check out the debug console (Cmd + Shift + R) when its running in the Simulator.

      If you still have issues drop a reply.

  16. dhanunjaya says:

    Hi ,

    it’s awesome tutorial and its helped me a lot ……

    I want to do one app that is read the data from xml and and put that in a table in sqlite and display that data in a simulator……can u please give me the idea how to do this app…..

    please help me…..

    Thanks
    Dhanunjaya M

  17. Thx for this tut. I’ll try to use the new knowlege for my own little test app now, but I fear that I have to dig deeper into core data for that ;-)

  18. Fellipe says:

    Great Article! Thanks!!!

  19. Great Job! Well done!
    Congratz.

  20. I fixed it, Forgot to #import the header file that declares TimeTableController’s interface.

    #import “TimeTableController.h” to “LapTimerAppDelegate.m”

  21. Ahbab says:

    really helpful

  22. Danny says:

    Great work.
    Can i set attribute with auto increment propert?

  23. Manaday says:

    Hey,

    Very great tutorial…

    Thanks.

  24. that is great tutorial…
    thank you…

  25. Rafik Samman says:

    Hi,

    I’m new to this, I’m running on ios sdk 4.2 (1750). I have a few issues with this source code.

    I can build and run properly, and the table view comes up properly showing the rows.

    At first, only the difference in time was showing when i was adding a lap time. Now neither the Lap time or the time difference shows up in the table view.

  26. Excellent introduction to Core Data. Thank you !!!

    • Ivo says:

      You have used “into” in your iexdend accessor name, where there should only be “in”.The method should be -insertObject:inBuddiesAtIndex:.

  27. Harpal Singh Sohi says:

    Thank You a lot..! I was thirsty to see this tutorial… Great ..

  28. Designzoo says:

    Helped me out big time! Great layout to the tutorial. I implemented this into a current project, so was unable to use the default xcode project as a base for this. to help anyone else out -

    Create an xcode core data example and ensure you copy all the code in the app delegate into your own project which initialises the context etc. – rename the sqllite and model filenames if you like
    Copy the datamodel file into your own project – it can be renamed if you desire (just check its renamed in finder)
    Then following the tutorial above and voila – easy storage on your iPhone project!

    If you are using core data in a view controller like this example I also added into viewDidLoad

    if (managedObjectContext == nil)
    {
    managedObjectContext = [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }

    prevents ‘+entityForName: could not locate an NSManagedObjectModel for entity name ‘Event” Error

  29. I had this idea in my head that it should be very complex. Apple managed to make it very friendly and accessible, and your article helps pointing that out with a basic example.

    Thanks!

    Now.. I need to figure out how I can manage more complex data, like instances of a class containing different objects themselves. :-)

  30. Pramod Jain says:

    I have few quests regarding the persistence of data .

    If the user deletes the application from iPhone device (not from the user iTunes library), then would these data will be available again after the user syncs/loads the same app on the device.

    AFAIK, it will not able to retrieve the prev. data, any how not sure as didn’t have a document relating to this detail.

    Please share info.

  31. James Dunay says:

    I’m not sure if anyone check this anymore but i still seem to be getting ‘+entityForName: could not locate an NSManagedObjectModel for entity name ‘Event” Error I’ll admit that I am trying to reuse this in an already created project but i have gone through it to make sure that everything is imported into the correct files. Any thoughts?

  32. MoTor says:

    Supernice, Thank you
    Did not download, just typing from the tutor and everything worked nice. Had to make some minor changes to the startup method (using ios 4.2)

  33. Clem says:

    Great tutorial, thanks.
    But, IMO you missed some code written in the LapTimerAppDelegate (definition of managedObjectContext, managedObjectModel, etc.) in this explanation. :)

  34. Michael says:

    This is a fantastic tutorial to Core Data. Everything is finally starting to make sense and I can actually begin to understand the documentation Apple provides. I don’t know why they had to make it seem so complex when the core concepts presented here are quite simple. Thanks for writing.

  35. Great tutorial! keep up the good work. I’ve been looking for a tutorial like this for a few days now. I’m glad I found it. It gave me a starting point to my next iPhone application. Thanks a lot!

  36. Bikshu says:

    Very nice tutorial….

  37. ima says:

    Very nice tutorial , I was looking for something exactly like this to get start on core data .I had to do some minor changes because I am using xcode 4.3.

    I was getting a blank screen ,so I had to use the following method instead of applicationDidFinishLaunching method.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //Rest of the code

    return YES;
    }
    .
    Keep up the good work

  38. maady says:

    its good but i want how image positions and images save in coredata database

  39. cem says:

    thanks a lot from Istanbul !

  40. Alex Mayfield says:

    I already started an app which is a drill view app and I need to store datasets from a web service in core data but I didn’t select use core data when I created it. Is there any way for me to change that in my pre-existing app? Or would I have to start over.

  41. mobibob says:

    This is a very nice consolidation of the massive Core Data Programming Guide. I am new to use the tools and I has started on 3.x, but now have to be on 4.x so I struggled to find the new menus for the tools. It would be super if you could reshoot your screenshots.

  42. Chris says:

    Great tutorial. There seems to be problems (blank screen) w/ this tutorial not working for xcode 4.0.2. I’m wondering if you’ve got the cycles to walk through it yourself and see if you can figure out what missing.

  43. Shillo says:

    Great tutorial, Iv’e managed to do everything except for a few things with Xcode 4.2 on iOS 5 beta 6, although I had to add some code and add some modifications to the view.
    If anyone needs help resolving some issues in order to run it with Xcode 4.2, feel free to contact me through my email.

  44. Jsdodgers says:

    is it possible to implement CoreData in an application that you already created if you did not check the “use Core Data for storage” box? If so, how?

  45. BearNun says:

    Yay for working source code! I was banging my head over why entering all this wasn’t working until I read someone’s suggestion at downloading it. So, I think the problem was:

    “@synthesize window, navigationController;”

    When I was trying to manually enter code and opened up the template for a table view it opened with a bunch of synthesizes like this:

    “@synthesize window = _window;”

    I read somewhere that this had useful purposes with memory management, but why does no tutorial seem to use it?

  46. Ravindra Chherke says:

    Great tutorial.Super Like :)

  47. durai says:

    Nice tutorial for programmers who are starting with core data.

    Thanks Alex Eckermann !

  48. Mohammed says:

    Many thanks it’s a very helpful tutorial .

  49. NMTZ says:

    Great tut, it helped me a lot, thanks =)

  50. durai says:

    Hi, will there be any new version of this tutorial for iOS 5 in XCode 4.2?.

    Thanks

Comment Page 1 of 21 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.