Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController's delegate doesn't fire after predicate is changed

I've been troubleshooting this issue for several hours now, no success so far. I've read all question here on SO that might be related to my problem.

I've got two Entities (A, B) that are connected to each other in a tree-like structure via a one-to-many-relationship like this: A <--->> B.

There's a UITableViewController backed by a NSFetchedResultsController to show all objects of entity B that are related to a selected object of entity A. I'm using the predicate for that:

    - (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"B" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"controlDate" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];

NSMutableArray *subpredicates = [NSMutableArray array];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"isRelatedTo = %@", selectedObjectOfA];
[subpredicates addObject:predicate1];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"syncStatus != %d", ObjectDeleted];
[subpredicates addObject:predicate2];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
[fetchRequest setPredicate:predicate];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _fetchedResultsController;
}

Everything works fine for adding, deleting and editing objects. The table gets updated accordingly. My class conforms to NSFetchedResultsControllerDelegate and the delegate methods are boilerplate.

But I want the user to be able to able to cycle through all objects of Entity A by tapping "up"/"down" buttons without going back in the navigation stack (like one can cycle through the emails directly in the Mail app). The number of relatedObjects of B varies depening on the selected Object of A (between none and and lets say 20).

And here comes the problem: My intention is to refresh the FRC's results by changing the predicate (not it's structure or syntax but only the value of the 'selectedObjectOfA' and calling

[self.fetchedResultsController performFetch:&error];

All the rest should be done by the FRC.

First question would be: is this approach correct?

Here are the scenarios I've already tried:

  • just setting 'selectedObjectOfA' to a new value and calling performFetch always shows the same result (because the fetchRequest and the predicate aren't being updated I suppose)

  • 'resetting' the FRC's fetchrequest like this

    NSMutableArray *subprediactes = [NSMutableArray array];
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"isRelatedTo = %@",    self.selectedObjectOfA];
    [subprediactes addObject:predicate1];
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"syncStatus != %d", ObjectDeleted];
    [subprediactes addObject:predicate2];
    
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subprediactes];
    [self.fetchedResultsController.fetchRequest setPredicate:predicate];
    

updates the FRC and returns the correct data as I suppose but the app crashes with a incorrect number of rows

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

because the NSFetchedResultsController delegate methods don't get called. Thus the number of rows in the tableView don't get adapted to the new results (the number of returned objects is correct according to the error message).

The second question is then: why does the delegate fire when adding/deleting objects but not after the predicates (and therefore the results) have changed?

Thank you in advance.

like image 757
Francesco Avatar asked Nov 08 '12 22:11

Francesco


2 Answers

You have to call

[tableView reloadData];

after modifying the fetch request. A fetched results controller tracks only changes that occurred after performFetch.

You could try the following to get an animated update of the table view (but I haven't tried that yet, so I am not sure if it works):

  • Call [tableView beginUpdates].
  • Use [tableView deleteRowsAtIndexPaths:...] to delete all "old" rows.
  • Update the fetched results controller fetch request.
  • Call [fetchedResultsController performFetch:...].
  • Call [tableView insertRowsAtIndexPaths:...] for all objects of [fetchedResultsController fetchedObjects] to insert all "new" rows.
  • Call [tableView endUpdates].
like image 150
Martin R Avatar answered Dec 15 '22 01:12

Martin R


To animate your tableview after predicate change:

NSFetchRequest* fetchRequest = self.fetchedResultsController.fetchRequest;
fetchRequest.predicate = newPredicate;

NSArray* objectsBefore = self.fetchedResultsController.fetchedObjects;

[self.fetchedResultsController performFetch:nil];

NSArray* objectsAfter = self.fetchedResultsController.fetchedObjects;


[self.tableView beginUpdates];

if (objectsBefore.count > 0)
{
    for (id objectBefore in objectsBefore)
    {
        if ([objectsAfter indexOfObject:objectBefore] == NSNotFound)
        {
            NSIndexPath* indexPath = [NSIndexPath indexPathForRow:[objectsBefore indexOfObject:objectBefore] inSection:0] ;

            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        }
    }
}

if (objectsAfter.count > 0)
{
    for (id objectAfter in objectsAfter)
    {
        if ([objectsBefore indexOfObject:objectAfter] == NSNotFound)
        {
            NSIndexPath* indexPath = [NSIndexPath indexPathForRow:[objectsAfter indexOfObject:objectAfter] inSection:0];

            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        }
    }
}

[self.tableView endUpdates];
like image 41
Maciek Czarnik Avatar answered Dec 15 '22 00:12

Maciek Czarnik