Crashing on saving a managedObjectContext, with &#

2019-04-25 09:21发布

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!

2条回答
狗以群分
2楼-- · 2019-04-25 10:06

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;
}
查看更多
我命由我不由天
3楼-- · 2019-04-25 10:12

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.

查看更多
登录 后发表回答