用好NSFetchedResultsController,程序的performance能大 大提高。原因在于你用NSFetchedResultsController去读取数据的话,苹果公司那些顶尖的工程师已经帮你想好了如何最大效率地读取 数据库。 所以,干脆利落地去掉NSArray和NSMutableArray,只用NSFetchedResultsController! 1,首先在h文件加入下面这段代码。
@interfaceFailedBanksListViewController:UITableViewController{
NSFetchedResultsController*_fetchedResultsController;
NSManagedObjectContext*_context;
}
@property(nonatomic, retain)
NSFetchedResultsController*fetchedResultsController;
@property(nonatomic, retain)NSManagedObjectContext*context;
@end
2,在m文件中,加入下面的代码
@synthesize fetchedResultsController = _fetchedResultsController;
3,在dealloc函数中,加入下面的代码:
self.fetchedResultsController.delegate=nil;
self.fetchedResultsController =nil;
4,在viewDidUnload函数中,加入下面的代码:
-(void)viewDidUnload {
self.fetchedResultsController =nil;
}
5,在m文件中,加入如下函数(以FailedBackInfo为数据对象)
-(NSFetchedResultsController*)fetchedResultsController {
if(_fetchedResultsController !=nil){
return _fetchedResultsController;
NSFetchRequest*fetchRequest =[[NSFetchRequest alloc] init];
NSEntityDescription*entity =[NSEntityDescription
entityForName:@"FailedBankInfo" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSSortDescriptor*sort =[[NSSortDescriptor alloc]
initWithKey:@"details.closeDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController*theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate=self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
上面的代码中,entity为实体对象,_context是coredata的NSManagedObject对象,它是连接数据库的一个上下文对象,相当于一个连接器。sort用来排序。cacheName用来组织数据成sections(这样组织方便后面展示) 6,在viewDidLoad中加入下面代码:
-(void)viewDidLoad {
[super viewDidLoad];
NSError*error;
if(![[self fetchedResultsController] performFetch:&error]){
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error,[error userInfo]);
exit(-1);// Fail
}
self.title =@"Failed Banks";
}
7,接下来,在UITableView中显示。
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return[sectionInfo numberOfObjects];
}
-(void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath {
FailedBankInfo*info =[_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = info.name;
cell.detailTextLabel.text =[NSString stringWithFormat:@"%@, %@", info.city, info.state];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
staticNSString*CellIdentifier=@"Cell";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell ==nil){
cell =[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
8,当数据发生变化时,coredata会自动通知显示层即UITableView作刷新。
-(void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
-(void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath {
UITableView*tableView =self.tableView;
switch(type){
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController*)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type){
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController*)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];
}