Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone -- breaking up Core Data into sections with NSFetchResultsController

So I have successfully implemented Core Data to retrieve objects from a server, save them, and display them in a UITableView. However now, I wish to break these up into separate sections. I have looked for a few days now, and NSFetchedResultsController seems to confuse me, even though the way I am using it works. I have a key in my Entity called "articleSection" that is set when the item is added to Core Data with items such as "Top" "Sports" "Life". How would I go about breaking these into separate sections in my UITableView? I have read about using multiple NSFetchedResultsControllers, but I am about as frustrated as can be with this.

Any suggestions or help would be greatly appreciated.

like image 909
Alex Muller Avatar asked Dec 05 '11 07:12

Alex Muller


1 Answers

The documentation for NSFetchedResultsController has sample code that works perfectly.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

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

    UITableViewCell *cell = /* get the cell */;
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    // Configure the cell with data from the managed object.
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

Set the sortDescriptors of the fetch request so the results are sorted by articleSection.
Set the sectionKeyPath to "articleSection" so the NSFetchedResultsController creates the sections for you. Something like this:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];;
request.fetchBatchSize = 20;
// sort by "articleSection"
NSSortDescriptor *sortDescriptorCategory = [NSSortDescriptor sortDescriptorWithKey:@"articleSection" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptorCategory, nil];;

// create nsfrc with "articleSection" as sectionNameKeyPath
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"articleSection" cacheName:@"MyFRCCache"];
frc.delegate = self;
NSError *error = nil;
if (![frc performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
self.fetchedResultsController = frc;
like image 56
Matthias Bauch Avatar answered Oct 03 '22 23:10

Matthias Bauch