|
| 1 | + |
| 2 | +/* |
| 3 | + File: DateSectionTitlesAppDelegate.m |
| 4 | + Abstract: Table view controller to display Events by section. |
| 5 | + The application delegate is almost unchanged from that provided by the standard Xcode template, except that application data is created in the persistentStoreCoordinator method. |
| 6 | + |
| 7 | + Version: 1.2 |
| 8 | + |
| 9 | + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
| 10 | + Inc. ("Apple") in consideration of your agreement to the following |
| 11 | + terms, and your use, installation, modification or redistribution of |
| 12 | + this Apple software constitutes acceptance of these terms. If you do |
| 13 | + not agree with these terms, please do not use, install, modify or |
| 14 | + redistribute this Apple software. |
| 15 | + |
| 16 | + In consideration of your agreement to abide by the following terms, and |
| 17 | + subject to these terms, Apple grants you a personal, non-exclusive |
| 18 | + license, under Apple's copyrights in this original Apple software (the |
| 19 | + "Apple Software"), to use, reproduce, modify and redistribute the Apple |
| 20 | + Software, with or without modifications, in source and/or binary forms; |
| 21 | + provided that if you redistribute the Apple Software in its entirety and |
| 22 | + without modifications, you must retain this notice and the following |
| 23 | + text and disclaimers in all such redistributions of the Apple Software. |
| 24 | + Neither the name, trademarks, service marks or logos of Apple Inc. may |
| 25 | + be used to endorse or promote products derived from the Apple Software |
| 26 | + without specific prior written permission from Apple. Except as |
| 27 | + expressly stated in this notice, no other rights or licenses, express or |
| 28 | + implied, are granted by Apple herein, including but not limited to any |
| 29 | + patent rights that may be infringed by your derivative works or by other |
| 30 | + works in which the Apple Software may be incorporated. |
| 31 | + |
| 32 | + The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
| 33 | + MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 34 | + THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
| 35 | + FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
| 36 | + OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
| 37 | + |
| 38 | + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
| 39 | + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 40 | + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 41 | + INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
| 42 | + MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
| 43 | + AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
| 44 | + STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
| 45 | + POSSIBILITY OF SUCH DAMAGE. |
| 46 | + |
| 47 | + Copyright (C) 2010 Apple Inc. All Rights Reserved. |
| 48 | + |
| 49 | + */ |
| 50 | + |
| 51 | +#import "DateSectionTitlesAppDelegate.h" |
| 52 | +#import "RootViewController.h" |
| 53 | + |
| 54 | +#import "Event.h" |
| 55 | + |
| 56 | +@interface DateSectionTitlesAppDelegate (CoreDateStack) |
| 57 | +@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; |
| 58 | +@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; |
| 59 | +@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; |
| 60 | +@end |
| 61 | + |
| 62 | + |
| 63 | +@implementation DateSectionTitlesAppDelegate |
| 64 | + |
| 65 | +@synthesize window; |
| 66 | +@synthesize navigationController; |
| 67 | + |
| 68 | + |
| 69 | +#pragma mark - |
| 70 | +#pragma mark Application lifecycle |
| 71 | + |
| 72 | +- (void)applicationDidFinishLaunching:(UIApplication *)application { |
| 73 | + |
| 74 | + RootViewController *rootViewController = (RootViewController *)[navigationController topViewController]; |
| 75 | + rootViewController.managedObjectContext = self.managedObjectContext; |
| 76 | + |
| 77 | + [window addSubview:[navigationController view]]; |
| 78 | + [window makeKeyAndVisible]; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +- (void)applicationWillTerminate:(UIApplication *)application { |
| 83 | + |
| 84 | + NSError *error = nil; |
| 85 | + if (managedObjectContext != nil) { |
| 86 | + if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { |
| 87 | + /* |
| 88 | + Replace this implementation with code to handle the error appropriately. |
| 89 | + |
| 90 | + 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. |
| 91 | + */ |
| 92 | + NSLog(@"Unresolved error %@, %@", error, [error userInfo]); |
| 93 | + abort(); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +#pragma mark - |
| 100 | +#pragma mark Core Data stack |
| 101 | + |
| 102 | +- (NSManagedObjectContext *) managedObjectContext { |
| 103 | + |
| 104 | + if (managedObjectContext != nil) { |
| 105 | + return managedObjectContext; |
| 106 | + } |
| 107 | + |
| 108 | + NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; |
| 109 | + if (coordinator != nil) { |
| 110 | + managedObjectContext = [[NSManagedObjectContext alloc] init]; |
| 111 | + [managedObjectContext setPersistentStoreCoordinator: coordinator]; |
| 112 | + } |
| 113 | + return managedObjectContext; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +- (NSManagedObjectModel *)managedObjectModel { |
| 118 | + |
| 119 | + if (managedObjectModel != nil) { |
| 120 | + return managedObjectModel; |
| 121 | + } |
| 122 | + managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; |
| 123 | + return managedObjectModel; |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { |
| 128 | + |
| 129 | + if (persistentStoreCoordinator != nil) { |
| 130 | + return persistentStoreCoordinator; |
| 131 | + } |
| 132 | + |
| 133 | + NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DateSectionTitles.sqlite"]]; |
| 134 | + |
| 135 | + |
| 136 | + BOOL firstRun = NO; |
| 137 | + if (![[NSFileManager defaultManager] fileExistsAtPath:[storeUrl path] isDirectory:NULL]) { |
| 138 | + firstRun = YES; |
| 139 | + } |
| 140 | + |
| 141 | + NSError *error = nil; |
| 142 | + persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; |
| 143 | + if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { |
| 144 | + /* |
| 145 | + Replace this implementation with code to handle the error appropriately. |
| 146 | + |
| 147 | + 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. |
| 148 | + */ |
| 149 | + NSLog(@"Unresolved error %@, %@", error, [error userInfo]); |
| 150 | + abort(); |
| 151 | + } |
| 152 | + |
| 153 | + /* |
| 154 | + If this is the first run, populate a new store with events whose timestamps are spaced every 7 days throughout 2009. |
| 155 | + */ |
| 156 | + if (firstRun) { |
| 157 | + NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; |
| 158 | + [context setPersistentStoreCoordinator:persistentStoreCoordinator]; |
| 159 | + |
| 160 | + NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; |
| 161 | + [dateComponents setYear:2009]; |
| 162 | + |
| 163 | + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; |
| 164 | + [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; |
| 165 | + [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; |
| 166 | + |
| 167 | + NSCalendar *calendar = [NSCalendar currentCalendar]; |
| 168 | + NSInteger day; |
| 169 | + |
| 170 | + for (day = 1; day < 365; day += 7) { |
| 171 | + |
| 172 | + [dateComponents setDay:day]; |
| 173 | + NSDate *date = [calendar dateFromComponents:dateComponents]; |
| 174 | + |
| 175 | + Event *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context]; |
| 176 | + newEvent.timeStamp = date; |
| 177 | + newEvent.title = [NSString stringWithFormat:@"%@ (day %d)", [dateFormatter stringFromDate:date], [dateComponents day]]; |
| 178 | + } |
| 179 | + |
| 180 | + [context save:NULL]; |
| 181 | + |
| 182 | + [context release]; |
| 183 | + [dateComponents release]; |
| 184 | + } |
| 185 | + |
| 186 | + return persistentStoreCoordinator; |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +#pragma mark - |
| 191 | +#pragma mark Application's Documents directory |
| 192 | + |
| 193 | +- (NSString *)applicationDocumentsDirectory { |
| 194 | + return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | +#pragma mark - |
| 199 | +#pragma mark Memory management |
| 200 | + |
| 201 | +- (void)dealloc { |
| 202 | + |
| 203 | + [managedObjectContext release]; |
| 204 | + [managedObjectModel release]; |
| 205 | + [persistentStoreCoordinator release]; |
| 206 | + |
| 207 | + [navigationController release]; |
| 208 | + [window release]; |
| 209 | + [super dealloc]; |
| 210 | +} |
| 211 | + |
| 212 | + |
| 213 | +@end |
| 214 | + |
0 commit comments