Skip to content

Commit 625e263

Browse files
committed
Added new sample: FetchedResultsControllerSample
1 parent 16c900f commit 625e263

File tree

13 files changed

+1942
-0
lines changed

13 files changed

+1942
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// FetchedResultsControllerSampleAppDelegate.h
3+
// FetchedResultsControllerSample
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/07/24.
6+
// Copyright Hiroshi Hashiguchi 2010. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <CoreData/CoreData.h>
11+
12+
@interface FetchedResultsControllerSampleAppDelegate : NSObject <UIApplicationDelegate> {
13+
14+
UIWindow *window;
15+
UINavigationController *navigationController;
16+
17+
@private
18+
NSManagedObjectContext *managedObjectContext_;
19+
NSManagedObjectModel *managedObjectModel_;
20+
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
21+
}
22+
23+
@property (nonatomic, retain) IBOutlet UIWindow *window;
24+
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
25+
26+
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
27+
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
28+
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
29+
30+
- (NSString *)applicationDocumentsDirectory;
31+
32+
@end
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//
2+
// FetchedResultsControllerSampleAppDelegate.m
3+
// FetchedResultsControllerSample
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/07/24.
6+
// Copyright Hiroshi Hashiguchi 2010. All rights reserved.
7+
//
8+
9+
#import "FetchedResultsControllerSampleAppDelegate.h"
10+
#import "RootViewController.h"
11+
12+
13+
@implementation FetchedResultsControllerSampleAppDelegate
14+
15+
@synthesize window;
16+
@synthesize navigationController;
17+
18+
19+
#pragma mark -
20+
#pragma mark Application lifecycle
21+
22+
- (void)awakeFromNib {
23+
24+
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
25+
rootViewController.managedObjectContext = self.managedObjectContext;
26+
}
27+
28+
29+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
30+
31+
// Override point for customization after application launch.
32+
33+
// Add the navigation controller's view to the window and display.
34+
[window addSubview:navigationController.view];
35+
[window makeKeyAndVisible];
36+
37+
return YES;
38+
}
39+
40+
41+
- (void)applicationWillResignActive:(UIApplication *)application {
42+
/*
43+
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
44+
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
45+
*/
46+
}
47+
48+
49+
- (void)applicationDidEnterBackground:(UIApplication *)application {
50+
/*
51+
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
52+
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
53+
*/
54+
}
55+
56+
57+
- (void)applicationWillEnterForeground:(UIApplication *)application {
58+
/*
59+
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
60+
*/
61+
}
62+
63+
64+
- (void)applicationDidBecomeActive:(UIApplication *)application {
65+
/*
66+
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
67+
*/
68+
}
69+
70+
71+
/**
72+
applicationWillTerminate: saves changes in the application's managed object context before the application terminates.
73+
*/
74+
- (void)applicationWillTerminate:(UIApplication *)application {
75+
76+
NSError *error = nil;
77+
if (managedObjectContext_ != nil) {
78+
if ([managedObjectContext_ hasChanges] && ![managedObjectContext_ save:&error]) {
79+
/*
80+
Replace this implementation with code to handle the error appropriately.
81+
82+
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.
83+
*/
84+
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
85+
abort();
86+
}
87+
}
88+
}
89+
90+
91+
#pragma mark -
92+
#pragma mark Core Data stack
93+
94+
/**
95+
Returns the managed object context for the application.
96+
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
97+
*/
98+
- (NSManagedObjectContext *)managedObjectContext {
99+
100+
if (managedObjectContext_ != nil) {
101+
return managedObjectContext_;
102+
}
103+
104+
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
105+
if (coordinator != nil) {
106+
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
107+
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
108+
}
109+
return managedObjectContext_;
110+
}
111+
112+
113+
/**
114+
Returns the managed object model for the application.
115+
If the model doesn't already exist, it is created from the application's model.
116+
*/
117+
- (NSManagedObjectModel *)managedObjectModel {
118+
119+
if (managedObjectModel_ != nil) {
120+
return managedObjectModel_;
121+
}
122+
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"FetchedResultsControllerSample" ofType:@"momd"];
123+
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
124+
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
125+
return managedObjectModel_;
126+
}
127+
128+
129+
/**
130+
Returns the persistent store coordinator for the application.
131+
If the coordinator doesn't already exist, it is created and the application's store added to it.
132+
*/
133+
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
134+
135+
if (persistentStoreCoordinator_ != nil) {
136+
return persistentStoreCoordinator_;
137+
}
138+
139+
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"FetchedResultsControllerSample.sqlite"]];
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+
Typical reasons for an error here include:
150+
* The persistent store is not accessible;
151+
* The schema for the persistent store is incompatible with current managed object model.
152+
Check the error message to determine what the actual problem was.
153+
154+
155+
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.
156+
157+
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
158+
* Simply deleting the existing store:
159+
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
160+
161+
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
162+
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
163+
164+
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
165+
166+
*/
167+
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
168+
abort();
169+
}
170+
171+
return persistentStoreCoordinator_;
172+
}
173+
174+
175+
#pragma mark -
176+
#pragma mark Application's Documents directory
177+
178+
/**
179+
Returns the path to the application's Documents directory.
180+
*/
181+
- (NSString *)applicationDocumentsDirectory {
182+
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
183+
}
184+
185+
186+
#pragma mark -
187+
#pragma mark Memory management
188+
189+
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
190+
/*
191+
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
192+
*/
193+
}
194+
195+
196+
- (void)dealloc {
197+
198+
[managedObjectContext_ release];
199+
[managedObjectModel_ release];
200+
[persistentStoreCoordinator_ release];
201+
202+
[navigationController release];
203+
[window release];
204+
[super dealloc];
205+
}
206+
207+
208+
@end
209+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// RootViewController.h
3+
// FetchedResultsControllerSample
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/07/24.
6+
// Copyright Hiroshi Hashiguchi 2010. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <CoreData/CoreData.h>
11+
12+
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
13+
14+
@private
15+
NSFetchedResultsController *fetchedResultsController_;
16+
NSManagedObjectContext *managedObjectContext_;
17+
18+
NSMutableArray* selectedIndexPahts_;
19+
}
20+
21+
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
22+
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
23+
@property (nonatomic, retain) NSMutableArray* selectedIndexPahts;
24+
25+
@end

0 commit comments

Comments
 (0)