0

我第一次使用核心数据概念来简单地插入和检索一些信息..

我有userInfoAppDelegate.huserInfoAppDelegate.mDisplayController.hDisplayController.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.hAND DisplayController.m

4

1 回答 1

0

在您的控制器中获取 AppDelegate 参考,然后在您使用的地方self.managedObjectContext,使用delegate.managedObjectContext

这是一个小样本

userInfoAppDelegate *delegate = (userInfoAppDelegate *)[[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
       inManagedObjectContext:delegate.managedObjectContext]; 
于 2012-11-30T06:39:07.740 回答