核心数据:在实体中找不到Keypath“objectID”

时间:2010-03-22 14:48:29

标签: iphone objective-c cocoa core-data nsfetchedresultscontroller

我正在使用带有谓词的NSFetchedResultsController来加载我的应用程序中的Documents列表。我想加载除当前活动的Documents以外的所有_Document

我使用Rentzsch的{​​{3}}来创建Document类,然后将所有自定义代码放在_Document子类中。 objectID生成DocumentID属性,其类型为currentDocID

在创建控制器的类中,我设置了控制器的controller.currentDocID = self.document.objectID; 属性:

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(objectID != %@)", self.currentDocID];
    [fetchRequest setPredicate:predicate];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateModified" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [sortDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}

在控制器本身,我懒得加载fetchedResultsController,如下所示:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath objectID not found in entity <NSSQLEntity Document id=1>'

当fetchedResultsController加载时,我的应用程序崩溃时出现未处理的异常:

{{1}}

我的理解是所有NSManagedObject都有一个objectID,无论是临时的还是永久的。这不是这种情况吗?有什么想法吗?

1 个答案:

答案 0 :(得分:10)

将谓词更改为

[NSPredicate predicateWithFormat:@"self != %@", [self currentDoc]]

其中currentDoc是对代表当前文档的NSManagedObject实例的引用。

核心数据将在内部进行相等性检查。