Skip to content

Instantly share code, notes, and snippets.

@matteom
Created November 19, 2013 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteom/7548487 to your computer and use it in GitHub Desktop.
Save matteom/7548487 to your computer and use it in GitHub Desktop.
Initializing the core data stack in the app delegate
@interface PCAppDelegate ()
@property PCPersistentStack *persistentStack;
@end
@implementation PCAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.persistentStack = [[PCPersistentStack alloc] initWithStoreURL:[self storeURL] modelURL:[self modelURL]];
UINavigationController* navigationController = (UINavigationController *)self.window.rootViewController;
PCLeadsViewController *leadsViewController = (PCLeadsViewController *)navigationController.topViewController;
leadsViewController.managedObjectContext = self.persistentStack.managedObjectContext;
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.persistentStack saveContext];
}
- (NSURL*)storeURL {
NSURL* documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
return [documentsDirectory URLByAppendingPathComponent:@"db.sqlite"];
}
- (NSURL*)modelURL {
return [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment