Core Data -existingObjectWithID:error: causes error 133000
The problem is that NSManagedObjectID
you pass is temporary. You can check it by calling NSManagedObjectID
's isTemporaryID
method. From docs:
Returns a Boolean value that indicates whether the receiver is temporary.
Most object IDs return NO. New objects inserted into a managed object context are assigned a temporary ID which is replaced with a permanent one once the object gets saved to a persistent store.
You should first save your changes to persistent store, only then get a permanent ID to pass to other context.
When you're using multiple contexts, you need to make sure you save context A before passing a managed object ID from context A to another context B. Only after the save completes will that object be accessible from context B.
-objectWithID:
will always return a non-nil object, but it will throw an exception once you start using it if there's no backing object in the store. -existingObjectWithID:error:
will actually run some SQL and do I/O if that object isn't already registered with the context it's used on.
NSManagedObjectReferentialIntegrityError = 133000
NSManagedObjectReferentialIntegrityError Error code to denote an attempt to fire a fault pointing to an object that does not exist. The store is accessible, but the object corresponding to the fault cannot be found. Available in Mac OS X v10.4 and later. Declared in CoreDataErrors.h.
See this documentation.
This tutorial might be helpful to you.
So the probable reason is you are trying to fetch the object which is non existing. This happens generally when you try to create an objectid for a non existing object. The objectid will be returned to you and when try to get the object with this objectId you are thrown this exception.