Step 1:
Right-click on "Supporting Files" and select "New File".
Step 2:
Choose "iOS" > "Core Data" > "Data Model" and click "Next".
Step 3:
Name the file and click "Save".
Step 4:
A new file will show up under Supporting Files.
Step 5:
Select it and the core data model editor will open.
Step 6:
Click the "Add Entity" button.
And name the Entity.
Step 7:
Click the "Add Attribute" button.
Name the attribute and set it's type.
Step 8:
Select the Contect entity.
Step 9:
Select "File" > "New File" from the menu bar.
Choose "Core Data" > "NSManagedObject subclass" and hit "Next".
After that click on core date and hit "Next".
After that click on "Contact entity" and hit "Next".
Step 10:
Click "Create" on the next screen; I just use the default location.
Step 11:
Add an import to the -prefix.pch file so you don't have to import Core Data whenever you need it.
Here we see Contact.h and Contact.m NSManagedObject class in the supporting file.
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 (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) coreDataViewController *viewController;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *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 defaultManager] URLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMask] lastObject];
}
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 alloc] init];
[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:nil] retain];
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 alloc] initWithManagedObjectModel:[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.