im first time using core data concept to simply insert and retrive some info..
i have userInfoAppDelegate.h , userInfoAppDelegate.m and DisplayController.h , DisplayController.m.
now i have succesfully addded and retrived data in my delegate files, here is code
userInfoAppDelegate.h
#import <UIKit/UIKit.h>
#import "HomeController.h"
@class RootViewController;
@interface UserInfoAppDelegate : UIResponder <UIApplicationDelegate>{
HomeController *hController;
UIWindow *Window;
}
@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) RootViewController *rootViewController;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
userInfoAppDelegate.m
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//some code...
UserInfo *newUser = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo"
inManagedObjectContext:self.managedObjectContext];
if (newUser != nil){
newUser.firstName = @"test";
newUser.lastName = @"test";
newUser.userName=@"b@test.com";
// newUser.bDate = [NSDate date] ;
newUser.department=@"Admin";
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError])
{
NSLog(@"Successfully saved the context.");
}
else
{
NSLog(@"Failed to save the context. Error = %@", savingError);
}
}
else {
NSLog(@"Failed to create the new person.");
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
/* Here is the entity whose contents we want to read */
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
inManagedObjectContext:self.managedObjectContext];
/* Tell the request that we want to read the contents of the Person entity */
[fetchRequest setEntity:entity];
NSError *requestError = nil;
/* And execute the fetch request on the context */
NSArray *users = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&requestError];
NSLog(@"%@",users);
if ([users count] > 0){
NSUInteger counter = 1;
for (UserInfo *thisUser in users){
NSLog(@"Person %lu First Name = %@", (unsigned long)counter, thisUser.firstName);
NSLog(@"Person %lu Last Name = %@", (unsigned long)counter,
thisUser.lastName);
NSLog(@"Person %lu Department = %@", (unsigned long)counter,
thisUser.department ); counter++;
}
}
else {
NSLog(@"Could not find any Person entities in the context.");
}
//some code...
}
// ABOVE CODE WORKED WELL WITH PROPER RESULT , BUT SAME CODE NOT WORKING WITH MY DisplayController.h AND DisplayController.m.
Get AppDelegate reference in your controller and then where you are using
self.managedObjectContext, usedelegate.managedObjectContextHere’s a small sample