Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Setup context and add created sqlite

  • Loading branch information...
commit 3693625809441e6ccab5fce3108b65f96cdb9537 1 parent 87dc130
@danielctull authored
View
73 SampleCoreData Project/SampleCoreData/main.m
@@ -16,9 +16,28 @@ int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
- // Create the managed object context
- NSManagedObjectContext *context = managedObjectContext();
+
+ NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
+
+
+ NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
+ path = [path stringByDeletingPathExtension];
+ NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];
+
+ NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
+
+ [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
+ configuration:nil
+ URL:url
+ options:nil
+ error:NULL];
+
+ NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
+
+ [context setPersistentStoreCoordinator:persistentStoreCoordinator];
+ [persistentStoreCoordinator release];
+
+
Person *dan = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
@@ -42,6 +61,7 @@ int main (int argc, const char * argv[])
dan.address = address;
martin.address = address;
+ NSLog(@"Saving to %@", url);
// Save the managed object context
NSError *error = nil;
@@ -54,50 +74,3 @@ int main (int argc, const char * argv[])
return 0;
}
-NSManagedObjectModel *managedObjectModel() {
-
- static NSManagedObjectModel *model = nil;
-
- if (model != nil) {
- return model;
- }
-
- NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
- path = [path stringByDeletingPathExtension];
- NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"mom"]];
- model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
-
- return model;
-}
-
-NSManagedObjectContext *managedObjectContext() {
-
- static NSManagedObjectContext *context = nil;
- if (context != nil) {
- return context;
- }
-
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- context = [[NSManagedObjectContext alloc] init];
-
- NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel()];
- [context setPersistentStoreCoordinator: coordinator];
-
- NSString *STORE_TYPE = NSSQLiteStoreType;
-
- NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
- path = [path stringByDeletingPathExtension];
- NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];
-
- NSError *error;
- NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];
-
- if (newStore == nil) {
- NSLog(@"Store Configuration Failure %@",
- ([error localizedDescription] != nil) ?
- [error localizedDescription] : @"Unknown Error");
- }
- [pool drain];
- return context;
-}
-
View
BIN  SampleCoreData.sqlite
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.