I am using RestKit in my iOS app and up until now I was simply using object mapping because I wasn't persisting any data to CoreData. I now want to add the possibility for the users to download some data, and use that data if the user has no internet connection.
I understand I have to use Entity Mapping for that, but I'm running into an issue: how can I use two different mappings for the same request? I mean, I don't understand how I am supposed to handle these two cases. Whether the users decided to download the data or is just requesting it to display once, the URL path will be exactly the same. How can I tell RestKit once to store it in CoreData and the other time to simply map it with ObjectMapping?
Basically, I'm asking the same question as this one: How to use Core Data models without saving them? but specifically for RestKit instead of MagicalRecords.
The right way to handle this case is to use different ManagedObjectContexts. You will need one for the persistent data, and it can be set up like this:
// Initialize managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
[managedObjectStore createPersistentStoreCoordinator];
[[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
Then, you can create a second context, that will be temporary only:
NSManagedObjectContext *newTemporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; // Choose your concurrency type, or leave it off entirely
[newTemporaryContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
newTemporaryContext.persistentStoreCoordinator = coordinator;
Finally, once this is done, you should store a reference to your temporary context somewhere, and decide on which context to used, based on the context of your app.