ios 如何从特定对象 ID 获取核心数据对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5035057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Core Data object from specific Object ID?
提问by Jason
I can easily get an object's ID in Core Data using the following code:
我可以使用以下代码轻松获取 Core Data 中的对象 ID:
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:
但是,有没有办法通过给它一个特定的对象 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];
However, I'd like to do it in a way that does not initiate its own fetch request. Any ideas?
但是,我想以一种不启动自己的获取请求的方式来做。有任何想法吗?
回答by rgeorge
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.
从具有该 ID 的商店中获取对象,如果不存在则为 nil。
(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:
(请注意:NSManagedObjectContext 上有两个方法看起来很相似,这让我很困惑。为了让它们保持直截了当,下面是另外两个方法:
-(NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID
...will create a fault object with the provided objectID, whether or notsuch 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.
...将使用提供的 objectID 创建一个错误对象,无论此类对象是否实际存在于商店中。如果它不存在,则任何触发错误的操作都将失败,除非您首先使用 NSManagedObjectContext 的insertObject:
. 我发现的唯一用途是将对象从一个存储复制到另一个存储,同时保留 ObjectID。
-(NSManagedObject *)objectRegisteredForID:(NSManagedObjectID *)objectID
...will return the object that has that ID, ifit has been fetched from the store by this managedObjectContext. If anyone knows what this method is useful for, please comment.)
...将返回具有该 ID 的对象,如果该对象已通过此 managedObjectContext 从存储中获取。如果有人知道这种方法有什么用,请发表评论。)
[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.]
[eta.:第一种方法和其他两种方法之间的另一个重要区别是existingObjectWithID:error:
从不返回错误;它总是为你获取整个对象。如果您试图避免这种情况(例如,使用具有大 blob 属性的昂贵的获取对象),您必须巧妙地使用objectWithID:
or objectRegisteredForID:
,它不会触发错误;或使用正确配置的获取请求。]
回答by quellish
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.
objectWithID:
是您正在寻找的方法,这是执行此操作的推荐方法。objectWithID:
将有效地使用 NSManagedObjectContext 仅根据需要拉取对象的级别 - 与执行此操作的其他一些方法不同。objectWithID:
在进入后备存储之前,将正确使用父上下文、持久存储协调器和持久存储本身中的内存信息。
This is covered in depth in the WWDC 2012session "Core Data Best Practices".
WWDC 2012会议“核心数据最佳实践”对此进行了深入介绍。
回答by Antonín Karásek
Swift 5 version:
斯威夫特 5 版本:
https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506686-existingobject
https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506686-existingobject
there are also methods object(with:)
or registeredObject(for:)
. Depending on what you need.
还有方法object(with:)
或registeredObject(for:)
。取决于你需要什么。