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

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

问题描述

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

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

NSManagedObjectID *moID = [managedObject objectID];

但是,有没有办法通过给它一个特定的对象 ID 来将对象从核心数据存储中取出?我知道我可以通过使用 NSFetchRequest 来做到这一点,如下所示:

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?

推荐答案

您想要:

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

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

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

(请注意:NSManagedObjectContext 上有两个名称相似的方法让我感到困惑.为了让它们保持正确,以下是另外两个方法:

(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

...将使用提供的 objectID 创建一个错误对象,无论是否这样的对象确实存在于商店中.如果它不存在,则任何触发错误的操作都将失败,除非您首先使用 NSManagedObjectContext 的 insertObject: 插入对象.我发现的唯一用途是将对象从一个存储复制到另一个存储,同时保留 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

...将返回具有该 ID 的对象,如果它已被此 managedObjectContext 从存储中提取.如果有人知道这个方法有什么用,请评论.)

...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.: 第一种方法和其他两种方法之间的另一个重要区别是 existingObjectWithID:error: 从不返回错误;它总是为你获取整个对象.如果您想避免这种情况(例如,使用具有大 blob 属性的获取成本高昂的对象),则必须巧妙地使用 objectWithID:objectRegisteredForID:,不会触发故障;或使用正确配置的获取请求.]

[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 获取核心数据对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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