开发者

What's the difference between -existingObjectWithID:error: and –objectWithID:?

What's the difference between 开发者_StackOverflow社区-existingObjectWithID:error: and –objectWithID: ?


objectWithID: assumes the object exists so if you give it a bad ID it will throw an exception when you attempt to access a property on the returned entity. Also, this method always returns an entity.

existingObjectWithID:error: will return an object if it exists and nil if it does not. If there was an error it will populate the error pointer.


I believe existingObjectWithID:error: returns an instance only if it has already been registered with the receiving context. objectWithID: may return a fault for an object that has not yet been registered (pulled into) the receiving context.


existingObjectWithID:error: will return the object if it has been registered - that is, it was created in this NSManagedObjectContext, or has been fetched (or retrieved using objectWithID:). It will return nil otherwise. A managed object that is in the datastore but has never been retrieved by this context cannot be found by this method. This method will never throw an exception if it gets a bad id or an id for an object that has been deleted.

A managed object that is in the datastore but has not been retrieved can be faulted in by objectWithID: - it now will be registered and can be accessed. If the objectID is bad or the object has been deleted an exception will be raised.

It is important to note that the object id WILL CHANGE on first save (from a temporary to a permanent id), so don't expect to create an object, keep a reference to its original id and then retrieve it using that id after saving. You can call isTemporaryID to find out if the object id will change after saving.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜