移动核心数据持久存储时,为什么多对多关系消失?

时间:2015-01-21 23:38:47

标签: ios objective-c core-data core-data-migration ios-app-group

我有一个使用Core Data来保存信息的应用程序。我正在为我的应用程序添加共享扩展,这将需要访问相同的数据,因此我需要将我的持久存储从我的应用程序的沙箱移动到应用程序组沙箱中。正如Update Core Data store location to support App Groups中所建议的那样,我正在使用迁移来执行此操作。问题是我的多对多关系在迁移过程中消失了。

我能够从具有核心数据的基本单一视图应用程序开始重新创建问题。连接按钮以在默认的managedObjectContext(我必须添加队列)中创建对象,并显示状态消息:

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";
        });
    }

}];

在模拟器中运行时,我可以在命令行上使用sqlite3来检查SQLite数据库,特别是在添加这些对象后,核心数据创建的Z_1TOPICS链接表:

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

所有看起来都是正确的。接下来,设置第二个按钮以将持久性存储迁移到App Group目录:

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";
            });
        }
    }
}

大部分代码都直接来自上面提到的问题。书籍和主题之间的关系不会迁移,但对象本身是:

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

确实,在运行所有这些之前使用“-com.apple.CoreData.SQLDebug 1”启动参数,我可以验证Core Data是写入ZTOPICS和ZBOOKS表,但是从不写入Z_1TOPICS表。

所以现在我被卡住了。我甚至不确定我能做些什么来调查这个问题 - 可能会执行自定义迁移,以便我可以观察(甚至手动执行)关系迁移?我可能只是尝试手动移动持久存储文件(即使这是一个否 - 否),因为我不知道我还能做什么。谢谢你的时间。

0 个答案:

没有答案