Question

I am using the latest Restkit 0.20.3

Currently, I am creating my managed mappings like so:

RKEntityMapping *messagesAddedMapping = [RKEntityMapping mappingForEntityForName:@"Message" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];

But what if I want to creat a mapping that does not save this entity to core data?

If I do:

RKEntityMapping *messagesAddedMapping = [RKEntityMapping mappingForClass:[Message class]];

I get an error:

'NSInternalInconsistencyException', reason: 'You must provide a managedObjectStore. Invoke mappingForClass:inManagedObjectStore: instead.'

I need a way to make the API call to get the data, then perform the mapping and in the success block I have to do a few things manually and manually save the entity to core data.

My problem is that I have a Message entity which I normally just add messages to as new messages arrive, but the new API I am going to be consuming has 3 properties: messagesToAdd [message array], messagIdsToRemove [string array], messagesModified [message array].

So, I am creating a new object MessagePayload which has the 2 relationships: messagesAdded (Message) and messagesModified (Message) and also has messageIdsToDelete (transformable).

And for the MessagePayload, I don't want any of it stored. In the success block I just want to add new messages, update modified messages, and delete from the delete list.

Was it helpful?

Solution

You can't have a mapping that creates a managed object but doesn't save it into the context. You are free to edit the returned objects and then re-save from the success block if you want to.

If that isn't an option for some reason, you can create a mapping to a custom object or a dictionary and then convert to a managed object and save yourself.

If you choose to use a dictionary you can easily update your managed object with the KVC method setValuesForKeysWithDictionary:.


From your comment below, take a step back and think a little:

  1. Do I need to gather all of this data in 1 go (can I use multiple response descriptors)?
  2. Have I got my unique identifiers setup so RestKit can automatically update for me?
  3. Does everything need to go into Core Data?

While I don't know about your unique identifiers (you should have them setup), you can use 2 or 3 response descriptors to map this content. RestKit can automatically handle new and updated objects direct into the context. For your deletions, map these to a custom object or an NSMutableArray, then delete the objects from the success block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top