Eyes, JAPAN Blog > Including Core Data in existing iOS project

Including Core Data in existing iOS project

will

この記事は1年以上前に書かれたもので、内容が古い可能性がありますのでご注意ください。

Core Data is not new for many iOS developers. It was introduced in Mac OS X 10.4 Tiger and iOS 3.0. It provides possibility to include data store functions quickly to Mac OS App and iOS app.

There are already official manuals from Apple and a lot of blogs from other iOS developers[1][2].

This blog is aimed at including the necessary informations to have a fast usage of Core Data in existing iOS app.

1. Create Data Model of Core Data
File >> New >> File, select Core Data >> Data Model. A new xcdatamodeld file will be included in the project.
2. Include the CoreData.framework in the project
3. In the AppDelegate.h, including the following code:

#import <CoreData/CoreData.h>
#define ApplicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

4. In the AppDelegate.m, including the following methods:


/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */
- (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 by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {
	
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    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 = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"RoomMap.sqlite"]];//you can decide the name of sqlite file
	
	NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    
	// Allow inferred migration from the original version of the application.
	NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
							 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
							 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
	
	if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        // Handle the error.
    }
	
    return persistentStoreCoordinator;
}

/**
 Returns the path to the application's documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
	
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

5. In AppDelegate.m, add these code in the – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

    // Override point for customization after application launch.
    NSManagedObjectContext  *context = [self managedObjectContext];
    managedObjectContext = context;

Finished to include the necessary code, you can start to use core data to store information.

References:
[1] Core Data on iOS 5 Tutorial: Getting Started
[2] Core Data Tutorial for iOS

Comments are closed.