类 NSFetchedResultsController详解

本文详细介绍了如何使用NSFetchedResultsController优化iOS应用的数据读取性能,通过减少内存消耗和提高效率,实现更流畅的应用体验。重点包括NSFetchedResultsController的初始化、属性设置、数据刷新机制及在UITableView中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用好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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值