I create a managed object save the context and post like so:
[[RKObjectManager sharedManager] postObject:tag mapResponseWith:tagMappingForPOST delegate:tagLoader];
The tagLoader gets the object back but fails to save in the RestKit’s context saying:
Failed to save managed object context after mapping completed: The operation couldn’t be completed. (Cocoa error 134030.)
NSUnderlyingException=Cannot update object that was never inserted.
I’m doing the same thing with the same backend server (Parse.com) with the same class and it’s working fine. Any clues as to the potential reasons why one would get a “Cannot update object that was never inserted.” error?
So it turns out that RestKit expects the context to be saved before sending the
postObject:mapResponseWith:delegatemessage toRKObjectManager. My issue was that I was saving the context right after thepostObject:mapResponseWith:delegatemessage (which I thought was ok because this would still be before the loader ever got a response back from the webservice. I was doing something like so:… then the
myObjLoaderwould get a response back and attempt to update properties inmyObj(createdAt, parse’s objectId, etc.) and throw an error saying that myObj doesn’t exist.I should really read through the RestKit code to confirm, but I’m pretty sure what’s happening is that
RKObjectManageris creating the background thread and the context from the managed object store at the point in time thepostObject:mapResponseWith:delegatemessage is received and never updates with any potential merges that might exists before the response comes back. To be honest, I expected the context to be created once the response was received.So the thing to do is to save the context before sending the
postObject:mapResponseWith:delegatemessage, like so: