在保存对象时执行使用MagicalRecord时出现错误访问
在保存对象时,我遇到了使用MagicalRecord时出现的问题。在保存对象时执行使用MagicalRecord时出现错误访问
节约使用上下文:
- (void)saveContext {
[[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"You successfully saved your context.");
} else if (error) {
NSLog(@"Error saving context: %@", error.description);
}
}];
}
这个原因导致我的应用程序随机崩溃,并EXEC_BAD_ACCESS上[[self MR_defaultContext] mergeChangesFromContextDidSaveNotification:notification];
我用另一种方法:
//get correct order based on indexPath
Order *orderToComplete = [self objectInOrdersAtIndex:indexPath.section];
//set order as completed
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
{
Order *localOrder = [orderToComplete inContext:localContext];
[localOrder setIsCompletedValue:YES];
}completion:^(BOOL success, NSError *error)
{
if(success)
NSLog(@"You successfully saved your context.");
else
NSLog(@"Error saving context: %@", error.description);
}];
但还是随机崩溃。
我的应用程序不是多线程的应用程序。 这里是一个镜头:
有没有人有任何想法?
我可以解决我的问题。但找不到确切的原因。
步骤,您应该遵循
- 清除所有的目标方法的代码,只是创建一个对象,并保存呢?确实保存?如果是这样的话:
- 添加你的代码并一步一步地测试你可以在你的代码中找到它,managedObjectStore将进入不一致状态。对我来说,这是因为调用委托方法。委托方法调用后,我放了
[NSManagedObjectContext saveContext]
。它固定
即时通讯使用2.2版 我可以找到问题发生的地方,但我不明白为什么! 但我在这里: 一个新项目将被推送到设备从网络,所以如果它是第一个记录单元格应自动被选中。然后我调用detailsView控制器委托setItems来更新它的项目。之后,KVO将通知有关更改并尝试更新tableView: 想象没有记录,一个新项目将被推送到设备,其子项目将通过代理方法添加到detailsViewController
self.subItems =子项目;
步骤: 1-更新视图 2-保存上下文
委托方法在每一个推调用两次。 然后KVO通知有一个子项目的变化NSKeyValueChangeSetting 第二次推后如果第二次推包含新的子项目KVO第一次通知有一个插入项目subItemsNSKeyValueChangeInsertion,然后如我所说的委托方法调用两次,KVO通知有一个变化NSKeyValueChangeSetting。在重新加载桌面应用程序崩溃后的这一步!
NSNumber *kind = [change objectForKey:NSKeyValueChangeKindKey];
//if we are setting new items to the items array; i.e. when we select a new row
if ([kind integerValue] == NSKeyValueChangeSetting) {
// adding the all the new items at top of the tableView
NSArray *newItems = [change valueForKey:@"new"];
[self.tableView reloadData];
}
//if we are adding one new order to the orders array
else if ([kind integerValue] == NSKeyValueChangeInsertion)
{
// adding the new order at top of the tableView
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
}
最后我尝试了一次调用这个委托来解决这个崩溃问题。但我无法理解为什么上述错误发生!
具有相同的问题。你解决了吗? – 2014-11-22 18:55:56