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

- 33,216
- 24
- 116
- 190

- 45,440
- 50
- 177
- 260
3 Answers
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.

- 107,306
- 24
- 181
- 206
-
2The documentation actually indicates that `existingObjectWithID:error:` will consult the persistent store if the object is not already in the current context. Only then if it cannot find an object will it be an error. This is the most explicit method for getting an existing object, versus `objectWithID:` which doesn't care if the object exists at all, and `objectRegisteredWithID:` which requires the object to be already loaded into the context. – Michael Merickel Dec 26 '13 at 22:17
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.

- 2,459
- 1
- 26
- 53

- 46,571
- 9
- 101
- 182
-
1I tried objectWithID: It does not throw any exception. See http://stackoverflow.com/questions/3013194/must-i-explicitely-enable-exceptions-in-xcode – dontWatchMyProfile Jun 10 '10 at 09:53
-
Your answer is not quite right. `objectWithID:` won't throw an exception. It may return fault, that may throw an exception. – Anton Belousov Jul 16 '19 at 13:50
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.

- 507
- 3
- 12
-
What if you're in a child context and the object is registered in the parent. ExistingObjectWithID on the child will return via the parent, even if the child context hasn't yet registered this object. Is that correct? – bandejapaisa May 03 '13 at 19:53
-
4The statement: "A managed object that is in the datastore but has never been retrieved by this context cannot be found by this method." is false. `existingObjectWithID:error:` looks for the object in the context, and if it doesn't find it, it will look in the context's parent context (and that context's parent context, etc) or the context's persistent store until it finds it. If it finds it, it faults it into the context and returns it. If it doesn't find it, it returns `nil`. – Peyman Jun 18 '14 at 21:50