Skip to content
Newer
Older
100644 181 lines (127 sloc) 6.37 KB
d3c0b9f Cheers, initial commit
= authored
1 //
2 // Hacker_NewsAppDelegate.m
3 // Hacker News
4 //
5 // Created by Mark Fayngersh on 4/12/10.
6 // Copyright __MyCompanyName__ 2010. All rights reserved.
7 //
8
9 #import "Hacker_NewsAppDelegate.h"
10
11
12 #import "RootViewController.h"
13 #import "DetailViewController.h"
14
15
16 @interface Hacker_NewsAppDelegate (CoreDataPrivate)
17 @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
18 @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
19 @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
20 - (NSString *)applicationDocumentsDirectory;
21 @end
22
23
24 @implementation Hacker_NewsAppDelegate
25
26 @synthesize window, splitViewController, rootViewController, detailViewController;
27
28
29 #pragma mark -
30 #pragma mark Application lifecycle
31
32 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
33
34 // Override point for customization after app launch
35 rootViewController.managedObjectContext = self.managedObjectContext;
36
37
38 // Add the split view controller's view to the window and display.
39 [window addSubview:splitViewController.view];
40 [window makeKeyAndVisible];
41
42 detailViewController.scrollView.hidden = YES;
43 [detailViewController.indicator stopAnimating];
44
45 return YES;
46 }
47
48 /**
49 applicationWillTerminate: saves changes in the application's managed object context before the application terminates.
50 */
51 - (void)applicationWillTerminate:(UIApplication *)application {
52
53 NSError *error = nil;
54 if (managedObjectContext != nil) {
55 if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
56 /*
57 Replace this implementation with code to handle the error appropriately.
58
59 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
60 */
908bc18 don't log in release builds
Zach Drayer authored
61 HNLog(@"Unresolved error %@, %@", error, [error userInfo]);
d3c0b9f Cheers, initial commit
= authored
62 abort();
63 }
64 }
65 }
66
67
68 #pragma mark -
69 #pragma mark Core Data stack
70
71 /**
72 Returns the managed object context for the application.
73 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
74 */
75 - (NSManagedObjectContext *) managedObjectContext {
76
77 if (managedObjectContext != nil) {
78 return managedObjectContext;
79 }
80
81 NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
82 if (coordinator != nil) {
83 managedObjectContext = [[NSManagedObjectContext alloc] init];
84 [managedObjectContext setPersistentStoreCoordinator: coordinator];
85 }
86 return managedObjectContext;
87 }
88
89
90 /**
91 Returns the managed object model for the application.
92 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
93 */
94 - (NSManagedObjectModel *)managedObjectModel {
95
96 if (managedObjectModel != nil) {
97 return managedObjectModel;
98 }
99 managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
100 return managedObjectModel;
101 }
102
103
104 /**
105 Returns the persistent store coordinator for the application.
106 If the coordinator doesn't already exist, it is created and the application's store added to it.
107 */
108 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
109
110 if (persistentStoreCoordinator != nil) {
111 return persistentStoreCoordinator;
112 }
113
114 NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Hacker_News.sqlite"]];
115
116 NSError *error = nil;
117 persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
118 if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
119 /*
120 Replace this implementation with code to handle the error appropriately.
121
122 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
123
124 Typical reasons for an error here include:
125 * The persistent store is not accessible;
126 * The schema for the persistent store is incompatible with current managed object model.
127 Check the error message to determine what the actual problem was.
128
129
130 If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
131
132 If you encounter schema incompatibility errors during development, you can reduce their frequency by:
133 * Simply deleting the existing store:
134 [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
135
136 * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
137 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
138
139 Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
140
141 */
908bc18 don't log in release builds
Zach Drayer authored
142 HNLog(@"Unresolved error %@, %@", error, [error userInfo]);
d3c0b9f Cheers, initial commit
= authored
143 abort();
144 }
145
146 return persistentStoreCoordinator;
147 }
148
149
150 #pragma mark -
151 #pragma mark Application's Documents directory
152
153 /**
154 Returns the path to the application's Documents directory.
155 */
156 - (NSString *)applicationDocumentsDirectory {
157 return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
158 }
159
160
161 #pragma mark -
162 #pragma mark Memory management
163
164 - (void)dealloc {
165
166 [managedObjectContext release];
167 [managedObjectModel release];
168 [persistentStoreCoordinator release];
169
170 [splitViewController release];
171 [rootViewController release];
172 [detailViewController release];
173
174 [window release];
175 [super dealloc];
176 }
177
178
179 @end
180
Something went wrong with that request. Please try again.