1

My database model has a MainCategory entity and a SpendingCategory entity. Here I group by the MainCategory.position. But if these positions are changed no changes happen in the results of this NSFetchedResultsController. How can I track these changes with this FetchedResultsController?

    self.managedObjectContext = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext;

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SpendingCategory"];

    NSSortDescriptor *mainCatPosition = [[NSSortDescriptor alloc]
                            initWithKey:@"belongsToMainCategory.position" ascending:YES];
    NSSortDescriptor *spendingCatPosition = [[NSSortDescriptor alloc]
                            initWithKey:@"position" ascending:YES];

    request.sortDescriptors = [NSArray arrayWithObjects:mainCatPosition,spendingCatPosition,nil];
    request.predicate = [NSPredicate predicateWithFormat:@"liveBudget = %@", [NSNumber numberWithBool:YES]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request
                                                                       managedObjectContext:self.managedObjectContext
                                                                         sectionNameKeyPath:@"belongsToMainCategory.position"
                                                                                  cacheName:nil];

EDIT:

- (void)controller:(NSFetchedResultsController *)controller
  didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex
     forChangeType:(NSFetchedResultsChangeType)type
{
    if(!self.reordering){
        if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
        {
            switch(type)
            {
                case NSFetchedResultsChangeInsert:
                    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                    break;

                case NSFetchedResultsChangeDelete:
                    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                    break;
            }
        }
    }
}
MichiZH
  • 5,587
  • 12
  • 41
  • 81

1 Answers1

0

You have to implement the NSFetchedResultsControllerDelegate. Implement the call back that deals specifically with sections:

controller:didChangeSection:atIndex:forChangeType:

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thx see my edit, thats the method in my CoreDataViewController (parent class of all my View controllers using NSFetchedResultsController). But nothing happens, even though method is called correctly. – MichiZH Jan 01 '14 at 12:13
  • It follows, that your if statements do not evaluate as expected. – Mundi Jan 01 '14 at 12:36
  • The delegate is fine. The problem is that (bizarrely, one of the strangest things in all of Apple) core data does not work with relationships - relationships do NOT update in fetched results controllers - only the "main" entity triggers updates. – Fattie Feb 29 '20 at 14:05