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

Mar*_*don 12 iphone cocoa core-data objective-c nsfetchedresultscontroller

我正在使用NSFetchedResultsController谓词来加载Documents我的应用程序中的列表.我想加载Documents除当前活动的所有之外的所有内容.

我使用Rentzsch的MOGenerator创建一个_Document类,然后我将所有自定义代码放在Document子类中._Document生成一个objectID类型的属性DocumentID.

在创建控制器的类中,我设置了控制器的currentDocID属性:

controller.currentDocID = self.document.objectID;
Run Code Online (Sandbox Code Playgroud)

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath objectID not found in entity <NSSQLEntity Document id=1>'
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*rra 10

将谓词更改为读取

[NSPredicate predicateWithFormat:@"self != %@", [self currentDoc]]
Run Code Online (Sandbox Code Playgroud)

currentDocNSManagedObject表示当前文档的实例的引用在何处.

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

  • 你知道为什么`... @"self!=%@",[self currentDoc]]`就像``@"(objectID!=%@)",self.currentDocID]`.根据这篇文章http://stackoverflow.com/a/2306550/647644它应该是等效的. (2认同)