I was confused too for about 4 or 5 hours :)
So.
Your inherited class of NSPersistentStore is "representation" of your remote data storage.
So, for the accessing remote data and saving/caching it locally you need to do the following
1) Create subclass of NSPersistentStore and setup it.
Like that:
YOURIncrementalStore *incrementalStore = [coordinator addPersistentStoreWithType:[YOURIncrementalStore type] configuration:nil URL:nil options:nil error:&error];
where coordinator your main NSPersistentStoreCoordinator
2) Then, you need other NSPersistentStoreCoordinator, that will "coordinate local representation(incrementalStore) and context of the external storage" and provide your local storage representaton (like SQLite DB URL) to it:
[incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]
But dont forget, that your new persistent store must know all your previous local state. So options dict will be:
NSDictionary *options = @{ NSInferMappingModelAutomaticallyOption : @YES,
NSMigratePersistentStoresAutomaticallyOption:@YES }
So, imho, i understand all internal work this way:
You request some data from external API. Parse it, then save to the context of your backingPersistentStoreCoordinator, then merge to the main one. So the states of all the contexts will be equal.
All of the previous text is based on work with AFIncrementalStore workaround.
My code to implement AFIncrementalStore with MagicalRecord:
- (void)addMRAndAFIS {
[MagicalRecord setupCoreDataStack];
NSURL *storeURL = [NSPersistentStore urlForStoreName:[MagicalRecord defaultStoreName]];
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator defaultStoreCoordinator];
NSError *error = nil;
NSArray *arr = coordinator.persistentStores;
AFIncrementalStore *incrementalStore = (AFIncrementalStore*)[coordinator addPersistentStoreWithType:[PTIncrementalStore type] configuration:nil URL:nil options:nil error:&error];
NSDictionary *options = @{ NSInferMappingModelAutomaticallyOption : @YES,
NSMigratePersistentStoresAutomaticallyOption:@YES };
arr = coordinator.persistentStores;
if (![incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
If we need to discuss the easiest way, you need just subclass NSIncrementalStore, setup it correctly(like i wrote), parse data, then create some context, save date to it, then save it and merge to parent context.
So you will have 2 Stores and 2 contexts, and 1 StoreCoordinator.
If i've made mistake somewhere, please refer to it.
ALSO, try: https://gist.github.com/stevederico/5316737