我正在使用core data
,并有UITableViewController
来加载core data
的数据。这是modal view
,如果我第三次加载模态视图,它会与EXC_BAD_ACCESS
崩溃。
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create and configure a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Ride" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array
NSSortDescriptor *sectionTitle = [[NSSortDescriptor alloc] initWithKey:@"dateStart" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sectionTitle, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management
[aFetchedResultsController release];
[fetchRequest release];
[sectionTitle release];
[sortDescriptors release];
return fetchedResultsController;
}//end
崩溃说它来自这条线:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Ride" inManagedObjectContext:managedObjectContext];
这是在viewDidLoad:
if (managedObjectContext == nil) {
managedObjectContext = [MyAppDelegate instance].managedObjectContext;
}
呈现模态视图:
History *history = [[[History alloc] init] autorelease];
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:history] autorelease];
[self presentModalViewController:nav animated:YES];
解雇模态:
- (void)done {
[self dismissModalViewControllerAnimated:YES];
}
提供EXC_BAD_ACCESS的堆栈跟踪:
现在,为了使用核心数据设置此视图,我遵循了Core Data Books
示例项目,我的代码看起来几乎相同。为什么在加载模态视图几次后会崩溃?
答案 0 :(得分:0)
好的,我想通了,我的managedObjectContext没有被保留,因为我没有使用self。所以,我通过这样做来修复它:
// Core Data
if (managedObjectContext == nil) {
self.managedObjectContext = [MyAppDelegate instance].managedObjectContext;
}