如何从特定的对象ID获取Core Data对象?
我可以使用以下代码轻松获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要:
从具有该 ID 的存储中获取对象,如果不存在则返回 nil。
(请注意:NSManagedObjectContext 上有两个方法,它们的名称看起来很相似,这让我很困惑。为了帮助保持它们的正确性,以下是另外两个方法的作用:
...将使用提供的 objectID 创建一个错误对象,是否或不这样的对象实际上不存在于存储中,除非您首先使用 NSManagedObjectContext 的
insertObject:
插入该对象,否则引发该错误的任何操作都将失败。我发现这是在保留 ObjectID 的同时将对象从一个存储复制到另一个存储...将返回具有该 ID 的对象,如果它已被此 ManagedObjectContext 从存储中获取。知道这个方法有什么用,请评论。)
[eta.:第一个方法和其他两个方法之间的另一个重要区别是
existingObjectWithID:error:
永远不会返回错误;它总是为您获取整个对象。如果您想避免这种情况(例如,使用具有大 blob 属性的昂贵的获取对象),则必须巧妙地使用objectWithID:
或objectRegisteredForID:
,不会引发故障;或使用正确配置的获取请求。]You want:
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:
...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....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 withobjectWithID:
orobjectRegisteredForID:
, which don't fire faults; or use a properly configured fetch request.]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".
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:)
orregisteredObject(for:)
. Depending on what you need.