问题描述
文档状态:
,在Apple的 Core Recipes示例应用程序中,该方法的结果用于填充NSFetchRequest,然后使用请求的结果,并带有注释效果:
and in Apple's 'Core Recipes' sample app the result of the method is used to populate an NSFetchRequest, and the request's result is then used, with comments to that effect:
// first get the object into the context
Recipe *recipeFault = (Recipe *)[context objectWithID:objectID];
// this only creates a fault, which may NOT resolve to an object (for example, if the ID is for
// an objec that has been deleted already): create a fetch request to get the object for real
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity: [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(self == %@)", recipeFault];
[request setPredicate: predicate];
我看过很多例子(其他人的代码和苹果的 iClass),其中 objectWithID
是直接使用的-意思是,它的属性可以被访问和愉快地处理。
I have seen many examples (other's code, and apple's 'iClass') where the result from objectWithID
is used directly - meaning, its properties are accessed and merrily processed along.
应该 objectWithID
总是被视为也许存在对象?
Should objectWithID
always be treated as a 'maybe-this-exists' object?
我问是因为我
推荐答案
Apple文档告诉您的内容不是假设存在该对象是持久性存储,只是因为返回了一个对象。
What the Apple documentation is telling you is not to assume that the object exists is the persistent store, just because an object is returned.
您可以像对待它一样对待它,访问它的属性等等,因为在后台Core Data将访问数据存储以服务您的请求。但是,如果对象在商店中不存在,则会出现异常。
You can treat it as though it does, accessing its properties and so on, because in the background Core Data will access the data store to service your request. However, if the object doesn't exist in the store, you'll get an exception.
( objectWithID:
返回给您)。
这篇关于使用NSManagedObjectContext的objectWithID的“正确”方法是什么:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!