NSManagedObjectContext在第二次调用时为零

每次我想多次访问Core Data时都会出错。 我有一个表视图,我正在尝试接收数据并显示它。

我的SecondViewController.h中有一个变量:

#import  #import "CooperTestModel.h" #import "Event.h" #import  @interface SecondViewController : UITableViewController { NSManagedObjectContext *managedObjectContext; NSMutableArray *eventArray; } @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; @property (nonatomic, strong) NSMutableArray *eventArray; - (void) fetchRecords; @end 

和SecondViewController.m

 #import "SecondViewController.h" @implementation SecondViewController @synthesize managedObjectContext, eventArray; 

在我的AppDelegate中,我得到一个设置为我的ViewController的对象:

 - (void)applicationDidFinishLaunching:(UIApplication *)application { SecondViewController *tableController = [[SecondViewController alloc] init]; tableController.managedObjectContext = [self managedObjectContext]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController]; [window addSubview: [self.navigationController view]]; [window makeKeyAndVisible]; } - (NSManagedObjectContext *) managedObjectContext { if (managedObjectContext != nil) { NSLog(@"managedObjectContext already in use. Returning instance."); return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext = [[NSManagedObjectContext alloc] init]; [managedObjectContext setPersistentStoreCoordinator: coordinator]; } NSLog(@"managedObjectContext nil. Returning new instance."); return managedObjectContext; } 

然后我在tableView中获取数据:

 - (void)fetchRecords { NSLog(@"Fetching records."); // Define our table/entity to use NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]; // Setup the fetch request NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entity]; // Define how we will sort the records NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [request setSortDescriptors:sortDescriptors]; // Fetch the records and handle an error NSError *error; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (!mutableFetchResults) { NSLog(@"Error fetching data."); } // Save our fetched data to an array [self setEventArray: mutableFetchResults]; NSLog(@"Records found: %i", [eventArray count]); } 

这适用于第一次尝试,但后来,我收到此错误:

 2013-09-07 19:55:03.715 CooperTest[2697:11603] managedObjectContext nil. Returning new instance. 2013-09-07 19:55:03.729 CooperTest[2697:11603] Fetching records. 2013-09-07 19:55:03.732 CooperTest[2697:11603] Records found: 3 2013-09-07 19:55:03.734 CooperTest[2697:11603] Rows in table: 3 2013-09-07 19:55:04.874 CooperTest[2697:11603] Fetching records. 2013-09-07 19:55:04.878 CooperTest[2697:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event'' libc++abi.dylib: terminate called throwing an exception 

有什么想法吗? 我没有设置新项目并勾选“使用核心数据”选项,因为它不在那里。 所以我在AppDelegate中添加了CoreData.framework和一些代码。 此外,模型和类是正确的(我能够在fetchRecords的末尾存储3条记录)。

最后我通过使用修复它

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; managedObjectContext = appDelegate.managedObjectContext; 

在ViewControlelr中,而不是从AppDelegate传递它。