Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController based Table View always fails on SECOND insert of entity

I am using an NSFetchedResultsController to manage displaying fetched managed objects in a table view that has one section. The table starts out empty and the user can add new entities to it using the UI. As it stands, the program always works when adding the first entity, and always crashes when adding a second. There is sometimes no error presented upon the crash and other times there are errors of differing types (some included below). Through log statements and tracing I see that the program crashes just after the NSFetchResultsController's delegate's controllerWillChangeContent (which calls [self.tableView beginUpdates];) method exits, but before any other method in my code is called. Here are some of the relavant parts of my code. Configuring the NSFetchedResultsController:

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

// Configure request's entity and predicate
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSString *expression = [NSString stringWithFormat:@"brewery.name LIKE \"%@\"", self.brewery.name];
NSPredicate *predicate = [NSPredicate predicateWithFormat:expression];
[fetchRequest setPredicate:predicate];
self.resultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                             managedObjectContext:self.managedObjectContext
                                                               sectionNameKeyPath:nil
                                                                        cacheName:nil];
self.resultsController.delegate = self;
[fetchRequest release];

NSError *error = nil;
BOOL success = [resultsController performFetch:&error];
if (!success) {
    NSLog(@"Error fetching request %@", [error localizedDescription]);
}

Adding new entity:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Beer" inManagedObjectContext:self.managedObjectContext];
Beer *beer = [[Beer alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
beer.name = beerName;
beer.brewery = self.brewery;

I have seen the warnings in the docs about problems displaying tables with one section, and I have used Apple's workaround for that to no avail. Those methods aren't getting called prior to the crash anyway.

Some of the errors I've received:

Serious application error.  Exception was caught during Core Data change processing: *** -[NSCFString compareObject:toObject:]: unrecognized selector sent to instance 0x4e808c0 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[CALayer compareObject:toObject:]: unrecognized selector sent to instance 0x4e53b80 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[UITextTapRecognizer controllerWillChangeContent:]: unrecognized selector sent to instance 0x4ca5d70 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[CALayer controllerWillChangeContent:]: unrecognized selector sent to instance 0x4e271a0 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[NSCFNumber countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x4c96ee0 with userInfo (null)

As you can see, the errors (when one was presented) are not consistent, even when no change to the code was made.

Can anyone figure out what I'm doing wrong?

like image 863
Drew C Avatar asked Feb 11 '10 03:02

Drew C


1 Answers

Your inconsistent errors point to a possible over-release issue. Especially since changing your delegates to just reload the data makes the issue go away. Personally I would invest the time to solve the issue rather than coding around it as you definitely having a code issue there that will probably show up later somewhere else.

I would do the following:

  1. Turn on NSZombie
  2. Add a breakpoint for objc_exception_throw
  3. Run in the debugger

When the exception occurs, look at the memory address and see what is being accessed and where. This will isolate the issue and tell you what is going on. It would also be helpful, if that does not reveal the issue, to post your NSFetchedResultsController delegate methods so I can see if there is anything strange in them.

Best to update the original question with the additional information.

like image 179
Marcus S. Zarra Avatar answered Sep 19 '22 04:09

Marcus S. Zarra