保存 ManagedObjectContext 时崩溃,并出现“NSInvalidArgumentException”,但只是偶尔发生
我不断从 ManagedObjectContext 上的 save: 命令中遇到崩溃。它甚至没有满足 NSLog 语句,所以我没有看到未解决的错误语句,所以我无法弄清楚问题可能是什么。它不会每次都会发生,但只是偶尔发生。
这是代码(基本上想要增加一个计数器):
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);
}
在控制台窗口中,我收到这样的消息:
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'
或这样:
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'
甚至这样:
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'
然后它显示第一次抛出时的调用堆栈,然后是一个通知(抛出一个后调用的终止) “NSException”、“[切换到进程 23501]”和“程序收到信号:“SIGABRT”。”
我认为该问题与 CoreData 有关,但我不确定我是否已清理我的构建和目标。我尝试锁定/解锁 ManagedObjectContext ,但它似乎没有帮助。
如果有任何关于从哪里开始寻找解决方案的想法,我们将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在释放
UIViewController
而没有释放其关联的NSFetchedResultsController
。NSFetchedResultsController
正在尝试通知其委托(很可能是您的UIViewController
)退出时的保存。Looks like you are releasing a
UIViewController
and not releasing its associatedNSFetchedResultsController
. TheNSFetchedResultsController
is trying to notify its delegate (most likely yourUIViewController
) of the save on exit.为了详细说明 Marcus 的答案,您需要确保当视图消失时将 NSFetchedResultsController 的委托清空:
To elaborate on Marcus' answer, you need to make sure you nil out the delegate for your NSFetchedResultsController when your view disappears: