确定,
这是我的第一篇有关stackoverflow的帖子。我几个小时一直把头撞在墙上。我讨厌输入问题;你可以相信我已经详尽地试图自己解决这个问题了。我已经对以下错误做了一些研究,但是我仍然无法解决它。话虽这么说,我是一个noob ios 6开发人员,并且核心数据存在问题。错误是:
“CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者中的错误.ALL或ANY运算符的左侧必须是NSArray或NSSet .with userInfo(null)“
现在,我的数据模型如下:我有两个实体(一对多关系)。关系是行为 - >事件,其中0到未发生的事件发生次数。
我做的是这个;
在behavior表视图控制器中,我填充了NSFetchedResultsController,如下所示:
- (NSFetchedResultsController *)fetchedResultsController {
NSLog(@"Started Fetching In Behaviors...");
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Behavior"
inManagedObjectContext:context];
[fetchRequest setEntity:entityDesc];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
return fetchedResultsController;
}
所有这些都很好,而且很笨拙。该表填充没有问题。现在,当我选择一个单元格时,我会实例化另一个UITableViewCell控制器,
{
NSLog(@"A value was selected from the table view");
IncidentsViewController *vc = [[IncidentsViewController alloc] initWithStyle:UITableViewStylePlain];
vc.managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
vc.parentRelationship = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Managed set on Incidents View Controller: %@", vc.managedObject);
NSString* name = [[NSString alloc] initWithString:[[vc.managedObject valueForKey:@"name"] description]];
NSLog(@"Name of selected object: %@", name);
[self.navigationController pushViewController:vc animated:YES];
}
上面的下一个IncidentsViewController基本上与fetchedResultsController完全相同。我会粘贴它以保持完整性。
- (NSFetchedResultsController *)fetchedResultsController2 {
NSLog(@"Started Fetching Resuts from Incidents View Controller...");
if (fetchedResultsController2 != nil) {
NSLog(@"fetchedResultsController not null");
return fetchedResultsController2;
}
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Incident"
inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entityDesc];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"when" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSLog(@"parent relationship: %@", self.parentRelationship);
NSLog(@"managed object: %@", self.managedObject);
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"ANY relationship == %@", self.parentRelationship]];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController2 = aFetchedResultsController;
/*
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = self;
*/
NSLog(@"Ending fetching results from incidents controller...");
return fetchedResultsController2;
}
我的问题只是以下内容:当我有行
时 [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"ANY relationship == %@", self.parentRelationship]];
在第二个视图控制器中,所有内容都会正确填充到表中(即,只有选定的特定行为的事件出现在列表中。如果此行存在,我尝试向事件添加新记录实体,抛出上面的错误。如果我删除这个特定的行,每个行为都会出现在列表中(这是预期的)。但是,当我尝试向事件表中添加一些内容时,实际上会抛出错误在BehaivorViewController中;这对我来说非常令人惊讶,因为我实例化了新的NSFetchedResultsController,AppDelegate和NSManagedObjectContext,这些对于每个相应的视图控制器来说都是本地的。在我看来,这两个是如何相互关联的一些如何,虽然我似乎无法弄清楚如何。对于那些对核心数据有所了解的人来说,是否可能会对此有所了解。我已将完整的项目发布到以下网址:
https://webshare.uchicago.edu/users/dansully/Public/Dirty.zip
有问题的行是IncidentsViewController.m中的第310行。
非常感谢你帮我回答我的问题。我将继续努力,如果我找到解决方案,我会报告。
答案 0 :(得分:4)
如果Incident只能有零个或一个父关系,您可能想尝试从谓词中删除ANY
。这是代码:
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"relationship == %@", self.parentRelationship]];