您的位置:首页 > Web前端

NSFetchedResultsController的使用

2015-04-22 15:11 337 查看
在CoreData为UITableView提供数据的时候,使用NSFetchedReslutsController能提高体验,因为用NSFetchedReslutsController去读数据的话,能最大效率的读取数据库,也方便数据变化后更新界面,

具体使用方法如下:

1.添加属性

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

@synthesize fetchedResultsController = _fetchedResultsController;

2. 重载- (NSFetchedResultsController *)fetchedResultsController初始化NSFetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {

return _fetchedResultsController;

}

self.fetchedResultsController = [[CoreDataManager getInstance] contactFetchedResultsController];

self.fetchedResultsController.delegate = self;

return _fetchedResultsController;

}

其中

- (NSFetchedResultsController *)contactFetchedResultsController {

BSHLOG(@"contactFetchedResultsController");

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

NSEntityDescription *entity = [NSEntityDescription entityForName:kCoreDataEntityContacts

inManagedObjectContext:self.managedObjectContext];

[request setEntity:entity];

NSSortDescriptor *recordsort = [NSSortDescriptor sortDescriptorWithKey:@"name"

ascending:NO];

[request setSortDescriptors:[NSArray arrayWithObjects:recordsort, nil]];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request

managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil

cacheName:nil];

return [controller autorelease];

}

注意:初始化NSFetchedResultsController 的时候,NSFetchRequest必须设置NSSortDescriptor

3. 在viewDidLoad中为NSFetchedResultsController获取值

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view from its nib.

self.titleNavigationItem.title = NSLocalizedString(@"contacts_title", nil);

[self fetchContacts];

}

- (void)fetchContacts {

NSError *error;

if (![self.fetchedResultsController performFetch:&error]) {

// Update to handle the error appropriately.

BSHLOG(@"[fetchSinaFriend] Unresolved error %@, %@", error, [error userInfo]);

//exit(-1); // Fail

}

}

4.在viewDidUnload和dealloc中释放

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

[self releaseUI];

}

- (void)dealloc {

[self releaseUI];

[super dealloc];

}

- (void)releaseUI {

self.tableView = nil;

self.titleNavigationItem = nil;

self.fetchedResultsController.delegate = nil;

self.fetchedResultsController = nil;

}

5.显示在UITableView中

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

int count = [sectionInfo numberOfObjects];

BSHLOG(@"numberOfRowsInSection count = %d", count);

return count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *reuseIdentifier = @"ContactsViewController";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:reuseIdentifier];

}

EntityContacts *contact = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.textLabel.text = contact.name;

return cell;

}

6.添加NSFetchedResultsControllerDelegate的方法,当数据发生变化时,自动刷新UITableView

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

[_tableView beginUpdates];

}

- (void)controller:(NSFetchedResultsController *)controller

didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath

forChangeType:(NSFetchedResultsChangeType)type

newIndexPath:(NSIndexPath *)newIndexPath {

switch(type) {

case NSFetchedResultsChangeInsert:{

[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

}

break;

case NSFetchedResultsChangeDelete:

[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];

break;

case NSFetchedResultsChangeUpdate:

[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

break;

case NSFetchedResultsChangeMove:

[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];

[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];

break;

}

}

- (void)controller:(NSFetchedResultsController *)controller

didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo

atIndex:(NSUInteger)sectionIndex

forChangeType:(NSFetchedResultsChangeType)type {

switch(type) {

case NSFetchedResultsChangeInsert:

[_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationTop];

break;

case NSFetchedResultsChangeDelete:

[_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationBottom];

break;

}

}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

[_tableView endUpdates];

}

转自:http://blog.csdn.net/cjhzcwhz/article/details/7783553
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: