Core Data allows you to add multiple persistent stores to a single NSPersistentStoreCoordinator
(each with a different configuration) name, thereby bringing them together in a single NSManagedObjectContext
. What I haven't been able to find out is how Core Data handles atomicity of a save operation for multiple stores.
Let's say I have two stores:
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] init];
[coordinator addPersistentStoreWithType:type configuration:@"A" URL:aURL options:nil error:NULL];
[coordinator addPersistentStoreWithType:type configuration:@"B" URL:bURL options:nil error:NULL];
NSManagedObjectContext *context = [[NSManageObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];
And then it's time to save I do that:
NSError *error = nil;
BOOL result = [context save:&error];
The documentation states that the sequence of events will be:
- Save store A
- Save store B
What if store A saves correctly, but store B cannot save for some reason? (e.g. the file on disk was deleted, or permissions made it read-only, that sort of thing). I cannot find any documentation detailing whether or not Core Data will then rollback the changes to store A.
It seems odd to me that the object graph would be left in an inconsistent state (i.e. one store updated, one not), but somewhat tricky and resource intensive to perform fully atomic saving across multiple stores. Would just really like some clarification here, perhaps from someone with more experience of the system!