3

这似乎是一个相当普遍的工作流程,我有一个问题,我不知道为什么。我有一个带有 NSFetchedResultsController 的 UITableView 提供行。为了添加一个新对象,我插入到上下文中,然后呈现一个新的视图控制器来编辑细节。添加按钮操作如下:

    -(IBAction)addNewIssueType:(id)sender
{
    IssueType *newUntitledType = [NSEntityDescription insertNewObjectForEntityForName:@"IssueType" inManagedObjectContext:self.managedObjectContext];
    newUntitledType.name = NSLocalizedString(@"Untitled Task", @"Name for newly created task");
    [self saveContext];

    self.editingType = newUntitledType;
    [self presentNameEditViewForType:newUntitledType];
}

我在 saveContext 方法出现异常崩溃,错误:

2013-05-11 16:25:35.990 应用程序 [18843:c07] CoreData:错误:严重的应用程序错误。在核心数据更改处理期间捕获到异常。这通常是 NSManagedObjectContextObjectsDidChangeNotification 观察者中的一个错误。ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet。与用户信息(空)

2013-05-11 16:25:35.992 应用程序 [18843:c07] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet。”

*首先抛出调用栈:

以通知观察者的建议为线索,我查看了我的 NSFetchedResultsController,大概是范围内 NSManagedObjectContext 的唯一观察者:

-(NSFetchedResultsController *)typesController
{
    if (_typesController) {
        return _typesController;
    }

    // Set up the fetched results controller.
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"IssueType" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Filter out the required types
    NSPredicate *exclude = [NSPredicate predicateWithFormat:@"NONE name in %@",excludedTypes];
    [fetchRequest setPredicate:exclude];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.typesController = aFetchedResultsController;

    NSError *error = nil;
    if (![_typesController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        [self failAlertWithMessage:nil forceAbort:YES];
    }

    return _typesController;
}

我发现,如果我注释掉创建和分配谓词的行,它不会崩溃。当然,我会展示一些我想要隐藏的对象。我看不出谓词本身有什么问题, fetch 确实返回了预期的对象,并按预期排除了 excludeTypes 数组中的那些。

保存上下文方法在编辑或删除现有对象时没有任何问题,仅在插入新对象时。

除了主线程之外,我没有在这里使用和线程。

4

1 回答 1

4

NONE是对多关系的别名,NOT ANY并且确实是对多关系的别名。你可能想要的是:

[NSPredicate predicateWithFormat:@"NOT name in %@", excludedTypes];
于 2013-05-11T21:52:01.613 回答