如何从特定的对象ID获取Core Data对象?

发布于 2024-10-18 07:53:40 字数 621 浏览 1 评论 0原文

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

NSManagedObjectID *moID = [managedObject objectID];

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

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

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

但是,我想以一种不启动自己的获取请求的方式来做到这一点。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始于初秋 2024-10-25 07:53:40

您想要:

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

从具有该 ID 的存储中获取对象,如果不存在则返回 nil。

(请注意:NSManagedObjectContext 上有两个方法,它们的名称看起来很相似,这让我很困惑。为了帮助保持它们的正确性,以下是另外两个方法的作用:

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

...将使用提供的 objectID 创建一个错误对象,是否或不这样的对象实际上不存在于存储中,除非您首先使用 NSManagedObjectContext 的 insertObject: 插入该对象,否则引发该错误的任何操作都将失败。我发现这是在保留 ObjectID 的同时将对象从一个存储复制到另一个存储

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

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

[eta.:第一个方法和其他两个方法之间的另一个重要区别是 existingObjectWithID:error: 永远不会返回错误;它总是为您获取整个对象。如果您想避免这种情况(例如,使用具有大 blob 属性的昂贵的获取对象),则必须巧妙地使用 objectWithID:objectRegisteredForID:,不会引发故障;或使用正确配置的获取请求。]

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.]

月亮是我掰弯的 2024-10-25 07:53:40

objectWithID: 是您正在寻找的方法,也是推荐的方法。 objectWithID: 将有效地使用 NSManagedObjectContext 仅根据需要拉取对象的级别 - 与执行此操作的其他一些方法不同。 objectWithID: 将在进入后备存储之前正确使用父上下文、持久存储协调器和持久存储本身中的内存信息。

WWDC 2012 会议“核心数据最佳实践”对此进行了深入介绍。

objectWithID: is the method you are looking for, and it is the recommended way to do this. objectWithID: will efficiently use the NSManagedObjectContext to pull the object only as many levels as needed - unlike some of the other means of doing this. objectWithID: will correctly use in-memory information in parent contexts, the persistent store coordinator, and the persistent store itself before going to the backing storage.

This is covered in depth in the WWDC 2012 session "Core Data Best Practices".

似狗非友 2024-10-25 07:53:40

Swift 5 版本:

https://developer.apple.com/documentation/coredata/ nsmanagedobjectcontext/1506686-existingobject

还有方法 object(with:)registeredObject(for:)。取决于你需要什么。

Swift 5 version:

https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506686-existingobject

there are also methods object(with:) or registeredObject(for:). Depending on what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文