Crashing on saving a managedObjectContext, with 'NSInvalidArgumentException', but only sporadically
Asked Answered
M

2

7

I keep getting crashes from a save: command on a managedObjectContext. It doesn't even fulfill the NSLog statement so I don't see the unresolved error statement, so I can't figure out what the problem might be. It doesn't happen every time, but only sporadically.

Here's the code (which basically wants to increment a counter):

 if ([[managedObject valueForKey:@"canSee"]boolValue]){
    int read = [[managedObject valueForKey:@"timesRead"] intValue] +1;
    [managedObject setValue:[NSNumber numberWithInt:read] forKey:@"timesRead"]; 


    NSError *error;
    if (![resultsController.managedObjectContext save:&error]) {  //<-- crashes on this line!
        NSLog(@"Unresolved Core Data Save error %@, %@", error, [error userInfo]);
        exit(-1);
    }

In the console window I get messages like this:

  2010-08-20 08:12:20.594 AppName[23501:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet controllerWillChangeContent:]: unrecognized selector sent to instance 0xe54f560'

or this:

  2010-08-20 08:12:20.594 AppName[23501:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet controllerWillChangeContent:]: unrecognized selector sent to instance 0xe54f560'

or even this:

  2010-08-19 23:09:59.337 AppName[761:307] Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  -[UITableViewLabel controllerWillChangeContent:]: unrecognized selector sent to instance 0x7f0a860 with userInfo (null)
  2010-08-19 23:09:59.356 AppName[761:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewLabel controllerWillChangeContent:]: unrecognized selector sent to instance 0x7f0a860'

Then it shows the call stack at first throw, followed by a notice (terminate called after throwing an instance of 'NSException', '[Switching to process 23501]' and 'Program received signal: “SIGABRT”.'

I think the problem has something to do with CoreData but I'm not sure. I've cleaned my build and targets and it doesn't seem to help. I've tried locking/unlocking the ManagedObjectContext and it doesn't help.

Any ideas here on where to start to look for a resolution would be greatly appreciated!

Metanephros answered 20/8, 2010 at 16:2 Comment(0)
O
16

Looks like you are releasing a UIViewController and not releasing its associated NSFetchedResultsController. The NSFetchedResultsController is trying to notify its delegate (most likely your UIViewController) of the save on exit.

Outweigh answered 20/8, 2010 at 16:59 Comment(2)
Yes, the problem is definitely that the fetched results controller is sending a message intended for a UITableView (or its datasource or delegate) to to objects that don't understand the message. It really doesn't have anything directly to do with Core Data.Receivership
Thanks very much to both of you... if I understand correctly, something (like UIViewController or datasource or delegate) is being released when it shouldn't be, so I should be trying to hunt down autorelease or release statements that shouldn't be there, or inserting a 'retain' statement to hold some object around long enough? (The NSFetchedResultsController never actually gets to the point of doing the saving.)Metanephros
M
5

To elaborate on Marcus' answer, you need to make sure you nil out the delegate for your NSFetchedResultsController when your view disappears:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    self.fetchedResultsController.delegate = nil;
}
Maltase answered 16/2, 2013 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.