2012-11-30 20 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

相關問題