IPhone - Partition Master Data with NSFetchResultsController

So, I have successfully implemented Core Data to retrieve objects from the server, save them and display them in a UITableView. However, now I want to break them into separate sections. I searched for a few days, and NSFetchedResultsController seems to bother me, although the way I use it works. I have a key in my Entity called "articleSection" that is set when an item is added to Core Data with items such as "Top" "Sport" "Life". How can I split them into separate sections in my UITableView? I read about using multiple NSFetchedResultsControllers, but I'm as frustrated as possible.

Any suggestions or help are welcome.

+6
source share
1 answer

The documentation for NSFetchedResultsController contains sample code that works just fine.

- (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 query to fetch so that the results are sorted using the article. Set sectionKeyPath to "articleSection" so that NSFetchedResultsController creates sections for you. Something like that:

 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; 
+17
source

Source: https://habr.com/ru/post/903080/


All Articles