核心数据:Keypath“objectID”在实体中找不到 [英] Core Data: Keypath "objectID" not found in entity

查看:163
本文介绍了核心数据:Keypath“objectID”在实体中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSFetchedResultsController 使用谓词在我的应用程序中加载文档的列表。我想加载除当前有效的文档。



我使用Rentzsch的 MOGenerator 创建一个 _Document 类,然后我把所有我的自定义代码在文档子类中。 _Document 生成类型为 DocumentID objectID 属性。 p>

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

  controller.currentDocID = self.document.objectID; 

在控制器本身,我延迟加载fetchedResultsController:

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

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

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

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@dateModifiedascending: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时,我的应用程序崩溃时出现未处理的异常:

  ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:'实体中未找到keypath objectID< NSSQLEntity Document id = 1> ;'

这是我的理解,所有NSManagedObjects有一个objectID,无论是临时还是永久。这不是这样吗?任何想法?

解决方案

更改您的谓词以阅读

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


$ b b

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



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


I'm using NSFetchedResultsController with a predicate to load a list of Documents in my application. I want to load all the Documents except the currently active one.

I am using Rentzsch's MOGenerator to create a _Document class and then I put all my custom code in the Document subclass. _Document generates an objectID property with type DocumentID.

In the class that creates the controller, I set the controller's currentDocID property:

controller.currentDocID = self.document.objectID;

In the controller itself, I lazy load the fetchedResultsController like this:

- (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;
}

When the fetchedResultsController loads, my app crashes with an unhandled exception:

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

It's my understanding that all NSManagedObjects have an objectID, whether temporary or permanent. Is this not the case? Any thoughts?

解决方案

Change your predicate to read

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

Where currentDoc is a reference to the instance of the NSManagedObject that represents the current document.

Core Data will do the equality check internally.

这篇关于核心数据:Keypath“objectID”在实体中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆