I can easily get an object's ID in Core Data using the following code:
我可以使用以下代码在核心数据中轻松获取对象的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?
但是,我希望以不启动自己的fetch请求的方式进行。什么好主意吗?
2 个解决方案
#1
197
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 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.
…将使用提供的objectID创建一个错误对象,无论该对象是否实际存在于存储中。如果它不存在,那么任何触发错误的操作都将失败,除非您首先使用NSManagedObjectContext的insertObject:插入对象。我发现的惟一用途是在保存objective的同时从一个商店复制到另一个商店。
-(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.)
…如果这个managedObjectContext从存储中获取了该ID,则返回具有该ID的对象。如果有人知道这个方法是有用的,请评论。
[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.]
(η。:第一个方法和另外两个方法之间的另一个重要区别是,existingObjectWithID:错误:决不返回错误;它总是为您获取整个对象。如果您试图避免这种情况(例如,使用一个具有大blob属性的昂贵获取对象),那么您必须使用objectWithID:或objectRegisteredForID:,它不会触发错误;或者使用正确配置的fetchrequest。
#2
2
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 2012 session "Core Data Best Practices".
在WWDC 2012年会议“核心数据最佳实践”中详细介绍了这一点。
#1
197
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 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.
…将使用提供的objectID创建一个错误对象,无论该对象是否实际存在于存储中。如果它不存在,那么任何触发错误的操作都将失败,除非您首先使用NSManagedObjectContext的insertObject:插入对象。我发现的惟一用途是在保存objective的同时从一个商店复制到另一个商店。
-(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.)
…如果这个managedObjectContext从存储中获取了该ID,则返回具有该ID的对象。如果有人知道这个方法是有用的,请评论。
[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.]
(η。:第一个方法和另外两个方法之间的另一个重要区别是,existingObjectWithID:错误:决不返回错误;它总是为您获取整个对象。如果您试图避免这种情况(例如,使用一个具有大blob属性的昂贵获取对象),那么您必须使用objectWithID:或objectRegisteredForID:,它不会触发错误;或者使用正确配置的fetchrequest。
#2
2
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 2012 session "Core Data Best Practices".
在WWDC 2012年会议“核心数据最佳实践”中详细介绍了这一点。