Created
January 11, 2012 11:16
-
-
Save quietcricket/1594228 to your computer and use it in GitHub Desktop.
Adding Core Data for Xcode 4 Projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Initialize the context. This is THE object you are going to use for all the data input and output. | |
// | |
NSManagedObjectContext* context=[[NSManagedObjectContext alloc] init]; | |
// | |
// Initialize the model file, some black magic and you don't need to specify the model file name. | |
// You can point to it manually but since they introduced "versioned" data model, it's hard to figure out what name or path you should use. | |
// | |
NSManagedObjectModel* model=[NSManagedObjectModel mergedModelFromBundles:nil]; | |
// | |
// The storage coordinator. I assume everyone would like to use sqlite, right? | |
// | |
NSPersistentStoreCoordinator* coord=[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; | |
// | |
// Get the document path of the app. The sqlite file will be saved here once it's created. | |
// | |
NSString* documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; | |
// | |
// Path to the sqlite file. | |
// | |
NSString* sqlitePath=[documentPath stringByAppendingString:@"/any-name-you-want-your-sqlite-to-be.sqlite"]; | |
// | |
// Point the store coordinator to the sqlite file | |
// | |
[coord addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil]; | |
// | |
// Link the context with the store coordinator and you are done. Now you own the properly setup context. | |
// | |
[context setPersistentStoreCoordinator:coord]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment