How to Add Core Data to an Existing Project

Step 1:

Right-click on "Supporting Files" and select "New File".

Step 2:

Choose "iOS" > "Core Data" > "Data Model" and click "Next".

add-core-data-in-iPhone.png

Step 3:

Name the file and click "Save".

create-core-data-in-iPhone.png

Step 4:

A new file will show up under Supporting Files.

supporting-file-in-iPhone.png

Step 5:

Select it and the core data model editor will open.

core-data-in-iPhone.png

Step 6:

Click the "Add Entity" button.

Entity-in-iPhone.png

And name the Entity.

entity-info-in-iPhone.png

Step 7:

Click the "Add Attribute" button.
Attribute-in-iPhone.png

Name the attribute and set it's type.

Attribute-info-in-iPhone.png

Step 8:

Select the Contect entity.

Step 9:

Select "File" > "New File" from the menu bar.
add-file-in-iPhone.png

Choose "Core Data" > "NSManagedObject subclass" and hit "Next".

NSManaged-object-in-iPhone.png

After that click on core date 
and hit "Next".

select-coreData-in-iPhone.png

After that click on "Contact entity" and hit "Next".

Select-contect-in-iPhone.png

Step 10:

Click "Create" on the next screen; I just use the default location.

select-target-in-iPhone.png

Step 11:

Add an import to the -prefix.pch file so you don't have to import Core Data whenever you need it.

supporting-file-in-iPhone.png

Here we see Contact.h and Contact.m 
NSManagedObject class in the supporting file.

add-core-data-framework-in-iphone.png

Step 12:

Declare private variables for NSManagedObjectContext, NSManagedObjectModel and NSPersistentStoreCoordinator in the app delegate header file.

#import <UIKit/UIKit.h>

@class coreDataViewController;

@interface coreDataAppDelegate : UIResponder <UIApplicationDelegate>

@property (strongnonatomicUIWindow *window;

@property (strongnonatomiccoreDataViewController *viewController;

@property (readonlystrongnonatomicNSManagedObjectContext *managedObjectContext;

@property (readonlystrongnonatomicNSManagedObjectModel *managedObjectModel;

@property (readonlystrongnonatomicNSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;

- (NSURL *)applicationDocumentsDirectory;

@end

Step 13:

Implement the actions you declared in the header file, in the app delegate implementation file.

- (void)saveContext

{

NSError *error = nil;

NSManagedObjectContext *managedObjectContext = self.managedObjectContext;

if (managedObjectContext != nil)

{

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

{

/*

Replace this implementation with code to handle the error appropriately.

abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

*/

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

}

}

- (NSURL *)applicationDocumentsDirectory

{

return [[[NSFileManager defaultManagerURLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMasklastObject];

}


Step 14:

Implement the necessary core data methods in the app delegate implementation file.

- (NSManagedObjectContext *)managedObjectContext

{

if (managedObjectContext != nil)

{

return managedObjectContext;

}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

if (coordinator != nil)

{

managedObjectContext = [[NSManagedObjectContext allocinit];

[managedObjectContext setPersistentStoreCoordinator:coordinator];

}

return managedObjectContext;

}

/**

Returns the managed object model for the application.

If the model doesn't already exist, it is created from the application's model.

*/

- (NSManagedObjectModel *)managedObjectModel

{

if (managedObjectModel != nil)

{

return managedObjectModel;

}

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nilretain];

return managedObjectModel;

}

/**

Returns the persistent store coordinator for the application.

If the coordinator doesn't already exist, it is created and the application's store added to it.

*/

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{

if (persistentStoreCoordinator != nil)

{

return persistentStoreCoordinator;

}

NSURL *storeURL = [[self applicationDocumentsDirectory]URLByAppendingPathComponent:@"CoreDataTabBarTutorial.sqlite"];

NSError *error = nil;

persistentStoreCoordinator = [[NSPersistentStoreCoordinator allocinitWithManagedObjectModel:[selfmanagedObjectModel]];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURLoptions:nil error:&error])

{

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

return persistentStoreCoordinator;

}


Step 15:
The Core data is now ready to go. You'll still need to get the managedObjectContext of whichever view controller you want to use it in and then implement the save or retrieve processes.

Up Next
    Ebook Download
    View all
    Learn
    View all