【发布时间】:2011-01-29 05:09:52
【问题描述】:
在我的 UITableViewController 类中,我有一个用于 NSFetchResultsController 的 getter 函数
-(NSFetchedResultsController*) fetchedResultsController {
if (_fetchedResultsController == nil) {
FlipPadAppDelegate* app = (FlipPadAppDelegate*) [[UIApplication sharedApplication] delegate];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:
[NSEntityDescription entityForName:@"FundClass" inManagedObjectContext:[app managedObjectContext]]];
NSSortDescriptor* sortdesp = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortdesp]];
[fetchRequest setFetchBatchSize:20];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[app managedObjectContext]
sectionNameKeyPath:nil
cacheName:@"_Stock"];
_fetchedResultsController.delegate = self;
[fetchRequest release];
[sortdesp release];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
return _fetchedResultsController;
}
不知何故,当应用程序运行到这一点时,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section];
NSLog(@"numberOfRowsInSection %@", [sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
它会触发EXC_BAD_ACCESS
sqllite 数据库中有两条记录。
为什么会有例外?是因为我将sectionNameKeyPath 设置为nil 吗?还是因为我没有给这个控制器设置视图的dataSource?
【问题讨论】:
标签: objective-c ios core-data