NSFetchedResultsController的使用

在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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值