3

行,

这是我第一次在 stackoverflow 上发帖。几个小时以来,我一直把头撞在墙上。我讨厌输入问题;你可以相信我已经竭尽全力地尝试自己解决这个问题。我已经对以下错误进行了一些研究,但是我仍然无法解决它。话虽这么说,我是一个新手 ios 6 开发人员,并且对核心数据有疑问。错误是:

“CoreData:错误:严重的应用程序错误。在核心数据更改处理期间捕获了异常。这通常是 NSManagedObjectContextObjectsDidChangeNotification 观察者中的错误。ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet。带有 userInfo (空值)”

现在,我的数据模型如下:我有两个实体(一对多关系)。关系是行为 -> 事件,其中有 0 到无限次事件发生。

我做的是这个;

在行为表视图控制器中,我填充了一个 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;

}

所有这一切都很好,很帅。该表被填充没有问题。现在,当我选择一个单元时,我会做什么,我实例化另一个uitaiteViewCell Controller,

{
    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]];

对核心数据有一点了解的人是否有可能对此发表意见。我已将我的完整项目发布到以下 URL:

https://webshare.uchicago.edu/users/dansully/Public/Dirty.zip

有问题的行是 IncidentsViewController.m 中的第 310 行。

非常感谢您帮助我回答我的问题。我将继续努力,如果我找到解决方案,我会报告。

4

1 回答 1

4

ANY如果事件只能有零个或一个父关系,您可能想尝试从谓词中删除。这是代码:

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"relationship == %@", self.parentRelationship]];
于 2013-03-14T08:40:30.420 回答