如何从特定对象ID获取Core Data对象? [英] How to get Core Data object from specific Object ID?

查看:161
本文介绍了如何从特定对象ID获取Core Data对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码轻松地在Core Data中获取对象的ID:

  NSManagedObjectID * moID = [managedObject objectID] ; 

但是,有一种方法可以通过给它一个特定的对象ID?我知道我可以这样做使用NSFetchRequest,像这样:

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

NSPredicate * predicate = [NSPredicate predicateWithFormat:@(objectID =%@),myObjectID];
[fetchRequest setPredicate:predicate];

但是,我想以不启动自己的fetch请求的方式来做。任何想法?

解决方案

您想要:

 code>  - (NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID 
错误:(NSError **)错误

$ b b

从具有该ID的商店获取对象,如果不存在,则为nil。



(注意: NSManagedObjectContext的方法,类似的名字让我起来,为了帮助保持它们直,这里是其他两个做的:

  - (NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID 

...将创建一个故障对象提供的objectID,是否这样的对象实际上存在于存储中,如果不存在,任何触发故障的任何东西都会失败,除非你先插入对象NSManagedObjectContext的 insertObject:。我发现的唯一用法是将对象从存储复制到存储,同时保留ObjectID。

 code>  - (NSManagedObject *)objectRegisteredForID:(NSManagedObjectID *)objectID 

...返回具有该ID(如果)的对象已通过此managedObjectContext从商店提取。如果有人知道这种方法是有用的,请评论。)



:第一种方法和另外两种方法之间的另一个重要区别是 existingObjectWithID:error:从不返回错误;它总是为你取得整个对象。如果你想避免这种情况(例如使用一个大blob属性的昂贵的fetch对象),你必须聪明与 objectWithID: objectRegisteredForID:,这不会触发故障;或使用正确配置的抓取请求。]


I can easily get an object's ID in Core Data using the following code:

NSManagedObjectID *moID = [managedObject objectID];

However, is there a way to get an object out of the core data store by giving it a specific object ID? I know that I can do this by using an NSFetchRequest, like this:

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

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(objectID = %@)", myObjectID];
[fetchRequest setPredicate:predicate];

However, I'd like to do it in a way that does not initiate its own fetch request. Any ideas?

解决方案

You want:

-(NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID
                                   error:(NSError **)error

Fetches the object from the store that has that ID, or nil if it doesn't exist.

(Be aware: there are two methods on NSManagedObjectContext with similar-seeming names that tripped me up. To help keep them straight, here's what the other two do:

-(NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID

...will create a fault object with the provided objectID, whether or not such an object actually exists in the store. If it doesn't exist, anything that fires the fault will fail unless you insert the object first with NSManagedObjectContext's insertObject:. The only use I've found for this is copying objects from store to store while preserving ObjectIDs.

-(NSManagedObject *)objectRegisteredForID:(NSManagedObjectID *)objectID

...will return the object that has that ID, if it has been fetched from the store by this managedObjectContext. If anyone knows what this method is useful for, please comment.)

[eta.: Another important difference between the first method and the other two is that existingObjectWithID:error: never returns a fault; it always fetches the whole object for you. If you're trying to avoid that (e.g. working with an expensive-to-fetch object with a big blob property), you have to be clever with objectWithID: or objectRegisteredForID:, which don't fire faults; or use a properly configured fetch request.]

这篇关于如何从特定对象ID获取Core Data对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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