Created
November 19, 2013 16:49
-
-
Save matteom/7548487 to your computer and use it in GitHub Desktop.
Initializing the core data stack in the app delegate
This file contains 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
@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