我第一次使用核心数据概念来简单地插入和检索一些信息..
我有userInfoAppDelegate.h
,userInfoAppDelegate.m
和DisplayController.h
,DisplayController.m
。
现在我已在我的委托文件中成功添加和重新获取数据,这里是代码
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...
}
//上面的代码工作得很好,但是同样的代码不能与我的DisplayController.h
和DisplayController.m
一起工作。
答案 0 :(得分:0)
在您的控制器中获取AppDelegate参考,然后在self.managedObjectContext
使用的位置,使用delegate.managedObjectContext
这是一个小样本
userInfoAppDelegate *delegate = (userInfoAppDelegate *)[[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
inManagedObjectContext:delegate.managedObjectContext];