I have an application that uses Core Data to persist information. I'm adding a Share Extension to my application, which will need to access the same data, so I need to move my persistent store from my app's sandbox into an app group sandbox. As recommended in Update Core Data store location to support App Groups, I'm using a migration to do that. The problem is that my many-to-many relationships disappear during that migration.
I was able to re-create the problem starting with a basic Single View Application with Core Data. A button is wired to create the objects in the default managedObjectContext (which I had to add a queue to), and show a status message:
AppDelegate * delegate = [UIApplication sharedApplication].delegate;
[delegate.managedObjectContext performBlock:^{
Book * book1 = [[Book alloc] initWithEntity:[NSEntityDescription entityForName:@"Book" inManagedObjectContext:delegate.managedObjectContext] insertIntoManagedObjectContext:delegate.managedObjectContext];
Book * book2 = [[Book alloc] initWithEntity:[NSEntityDescription entityForName:@"Book" inManagedObjectContext:delegate.managedObjectContext] insertIntoManagedObjectContext:delegate.managedObjectContext];
Topic * topic1 = [[Topic alloc] initWithEntity:[NSEntityDescription entityForName:@"Topic" inManagedObjectContext:delegate.managedObjectContext] insertIntoManagedObjectContext:delegate.managedObjectContext];
Topic * topic2 = [[Topic alloc] initWithEntity:[NSEntityDescription entityForName:@"Topic" inManagedObjectContext:delegate.managedObjectContext] insertIntoManagedObjectContext:delegate.managedObjectContext];
Topic * topic3 = [[Topic alloc] initWithEntity:[NSEntityDescription entityForName:@"Topic" inManagedObjectContext:delegate.managedObjectContext] insertIntoManagedObjectContext:delegate.managedObjectContext];
book1.title = @"Breadbaker's Apprentice";
book1.authorName = @"Peter Reinhart";
book2.title = @"Tartine Bread";
book2.authorName = @"Chad Robertson";
topic1.topicName = @"Bread";
topic1.topicPriority = [NSDecimalNumber decimalNumberWithString:@"0.9"];
[topic1 addBooksObject:book2];
topic2.topicName = @"Baking";
topic2.topicPriority = [NSDecimalNumber decimalNumberWithString:@"0.7"];
[topic2 addBooksObject:book1];
[topic2 addBooksObject:book2];
topic3.topicName = @"San Francisco";
topic3.topicPriority = [NSDecimalNumber decimalNumberWithString:@"0.2"];
[topic3 addBooksObject:book2];
NSError * saveError;
if(![delegate.managedObjectContext save:&saveError])
{
NSLog(@"Failed to save for some reason: %@", saveError.debugDescription);
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = [NSString stringWithFormat:@"Failed to save for some reason: %@", saveError.debugDescription];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = @"OK, added objects";
});
}
}];
When run in the simulator I can use sqlite3 on the command line to inspect the SQLite db—and specifically the Z_1TOPICS linking table Core Data creates—after adding those objects:
sqlite> .tables
ZBOOKS ZTOPICS Z_1TOPICS Z_METADATA Z_PRIMARYKEY
sqlite> SELECT * from Z_1TOPICS;
1|3
1|2
2|2
2|3
2|1
All looks about right. Next, a second button is setup to migrate the persistent store to the App Group directory:
AppDelegate * delegate = [UIApplication sharedApplication].delegate;
NSURL *newStoreURL = [[delegate groupDocumentsDirectory] URLByAppendingPathComponent:@"Migration_Bug_Demonstration.sqlite"];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Migration_Bug_Demonstration" withExtension:@"momd"];
NSManagedObjectModel * managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSPersistentStoreCoordinator *tempCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
NSURL *oldStoreURL = [[delegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"Migration_Bug_Demonstration.sqlite"];
NSPersistentStore *sourceStore = nil;
NSPersistentStore *destinationStore = nil;
NSDictionary *RWoptions = @{NSMigratePersistentStoresAutomaticallyOption: @YES,
NSInferMappingModelAutomaticallyOption: @YES};
NSDictionary * ROoptions = @{NSReadOnlyPersistentStoreOption:@YES,NSMigratePersistentStoresAutomaticallyOption: @YES,
NSInferMappingModelAutomaticallyOption: @YES};
NSError * error;
// Add the source store
if (![tempCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:oldStoreURL options:ROoptions error:&error]){
// Handle the error
NSLog(@"Error adding old store: %@ -- %@", error, error.userInfo);
}
else
{
sourceStore = [tempCoordinator persistentStoreForURL:oldStoreURL];
if (sourceStore != nil){
// Perform the migration
destinationStore = [tempCoordinator migratePersistentStore:sourceStore toURL:newStoreURL options:RWoptions withType:NSSQLiteStoreType error:&error];
if (destinationStore == nil){
NSLog(@"Error migrating store: %@ -- %@", error, error.userInfo);
// Handle the migration error
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = [NSString stringWithFormat:@"Failed to migrate: %@", error.debugDescription];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = @"OK, moved store";
});
}
}
}
Much of that code is straight out of the Question linked above. The relationships between Books and Topics are not migrated, but the objects themselves are:
sqlite> .tables
ZBOOKS ZTOPICS Z_1TOPICS Z_METADATA Z_PRIMARYKEY
sqlite> SELECT * FROM Z_1TOPICS;
sqlite> SELECT * FROM ZTOPICS;
1|2|1|0.2|San Francisco
2|2|1|0.7|Baking
3|2|1|0.9|Bread
sqlite> SELECT * FROM ZBOOKS;
1|1|1|Peter Reinhart|Breadbaker's Apprentice
2|1|1|Chad Robertson|Tartine Bread
Indeed, using the "-com.apple.CoreData.SQLDebug 1" launch argument before running all this, I can verify that Core Data is writing to the ZTOPICS and ZBOOKS tables, but never to the Z_1TOPICS table.
So now I'm stuck. I'm not even sure what I else I can do to investigate this problem—maybe perform a custom migration so I can watch (or even manually perform) the relationship migration? I might just try to move the persistent store files manually (even though that's a No-No), because I'm not sure what else I can do. Thanks for your time.