Skip to content

Commit 3c93837

Browse files
committed
Added New Sample
1 parent a94d2a8 commit 3c93837

20 files changed

+2263
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// NSFetchedResultControllerDelegateSampleAppDelegate.h
3+
// NSFetchedResultControllerDelegateSample
4+
//
5+
6+
7+
#import <UIKit/UIKit.h>
8+
9+
@interface NSFetchedResultControllerDelegateSampleAppDelegate : NSObject <UIApplicationDelegate> {
10+
11+
UIWindow *window;
12+
13+
NSManagedObjectContext *managedObjectContext;
14+
NSManagedObjectModel *managedObjectModel;
15+
NSPersistentStoreCoordinator *persistentStoreCoordinator;
16+
UINavigationController *navigationController;
17+
}
18+
19+
@property (nonatomic, retain) IBOutlet UIWindow *window;
20+
21+
22+
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
23+
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
24+
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
25+
26+
- (NSString *)applicationDocumentsDirectory;
27+
28+
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
29+
@end
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//
2+
// NSFetchedResultControllerDelegateSampleAppDelegate.m
3+
// NSFetchedResultControllerDelegateSample
4+
//
5+
6+
7+
#import "NSFetchedResultControllerDelegateSampleAppDelegate.h"
8+
9+
#import "RootViewController.h"
10+
11+
@implementation NSFetchedResultControllerDelegateSampleAppDelegate
12+
13+
14+
@synthesize window;
15+
16+
@synthesize navigationController;
17+
18+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19+
20+
// Override point for customization after application launch.
21+
// Add the navigation controller's view to the window and display.
22+
[window addSubview:navigationController.view];
23+
[window makeKeyAndVisible];
24+
return YES;
25+
}
26+
27+
- (void)applicationWillTerminate:(UIApplication *)application {
28+
29+
// Saves changes in the application's managed object context before the application terminates.
30+
NSError *error = nil;
31+
if (managedObjectContext != nil) {
32+
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
33+
/*
34+
Replace this implementation with code to handle the error appropriately.
35+
36+
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.
37+
*/
38+
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
39+
abort();
40+
}
41+
}
42+
}
43+
44+
- (void)dealloc {
45+
46+
[window release];
47+
[managedObjectContext release];
48+
[managedObjectModel release];
49+
[persistentStoreCoordinator release];
50+
[navigationController release];
51+
[super dealloc];
52+
}
53+
54+
/**
55+
Returns the managed object context for the application.
56+
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
57+
*/
58+
- (NSManagedObjectContext *) managedObjectContext {
59+
60+
if (managedObjectContext != nil) {
61+
return managedObjectContext;
62+
}
63+
64+
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
65+
if (coordinator != nil) {
66+
managedObjectContext = [[NSManagedObjectContext alloc] init];
67+
[managedObjectContext setPersistentStoreCoordinator: coordinator];
68+
}
69+
return managedObjectContext;
70+
}
71+
72+
73+
/**
74+
Returns the managed object model for the application.
75+
If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
76+
*/
77+
- (NSManagedObjectModel *)managedObjectModel {
78+
79+
if (managedObjectModel != nil) {
80+
return managedObjectModel;
81+
}
82+
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
83+
return managedObjectModel;
84+
}
85+
86+
87+
/**
88+
Returns the persistent store coordinator for the application.
89+
If the coordinator doesn't already exist, it is created and the application's store added to it.
90+
*/
91+
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
92+
93+
if (persistentStoreCoordinator != nil) {
94+
return persistentStoreCoordinator;
95+
}
96+
97+
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"NSFetchedResultControllerDelegateSample.sqlite"]];
98+
99+
NSError *error = nil;
100+
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
101+
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
102+
/*
103+
Replace this implementation with code to handle the error appropriately.
104+
105+
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.
106+
107+
Typical reasons for an error here include:
108+
* The persistent store is not accessible
109+
* The schema for the persistent store is incompatible with current managed object model
110+
Check the error message to determine what the actual problem was.
111+
*/
112+
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
113+
abort();
114+
}
115+
116+
return persistentStoreCoordinator;
117+
}
118+
119+
120+
#pragma mark -
121+
#pragma mark Application's Documents directory
122+
123+
/**
124+
Returns the path to the application's Documents directory.
125+
*/
126+
- (NSString *)applicationDocumentsDirectory {
127+
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
128+
}
129+
130+
- (void)awakeFromNib {
131+
132+
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
133+
rootViewController.managedObjectContext = self.managedObjectContext;
134+
}
135+
136+
@end
137+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// RootViewController.h
3+
// NSFetchedResultControllerDelegateSample
4+
//
5+
6+
7+
#import <CoreData/CoreData.h>
8+
9+
//@interface RootViewController : UIViewController <NSFetchedResultsControllerDelegate> {
10+
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
11+
12+
// UITableView* tableView_;
13+
NSFetchedResultsController *fetchedResultsController;
14+
NSManagedObjectContext *managedObjectContext;
15+
}
16+
17+
//@property (nonatomic, retain) IBOutlet UITableView* tableView;
18+
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
19+
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
20+
21+
@end
22+

0 commit comments

Comments
 (0)