2012-11-30 19 views
0

IM首次采用核心数据的概念简单地插入和retrive一些信息..核心数据在我的控制器中不起作用。如何将managedObjectContext链接到我的控制器?

我有userInfoAppDelegate.huserInfoAppDelegate.mDisplayController.hDisplayController.m

现在我已经成功地addded和我的委托文件retrived数据,这里是代码

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"; 
    [email protected]"[email protected]"; 
    // newUser.bDate = [NSDate date] ; 
    [email protected]"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.hDisplayController.m

+0

让我知道如果DisplayController代码所需。 – BaSha

回答

0

得到您的控制器的AppDelegate参考,然后你在哪里使用self.managedObjectContext,使用delegate.managedObjectContext

这里是一个小样本

userInfoAppDelegate *delegate = (userInfoAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo" 
     inManagedObjectContext:delegate.managedObjectContext]; 
+0

我需要将UserInfoAppDelegate.h导入到我的控制器,还是可以从所有控制器访问? – BaSha

+0

您需要将其导入到您想要使用它的地方 – aahsanali

+0

工作!尽管有些不妥。 可以解释这是什么意思? 'NSError * savingError = nil; ([self.managedObjectContext save:&savingError]) NSLog(@“Successfully saved the context。”); (@“ } else NSLog(@”无法保存上下文,错误=%@“,savingError); } – BaSha

相关问题