objective-c - 合并两个 iOS 核心数据持久存储的有效方法是什么?

标签 objective-c core-data merge core-data-migration object-graph

在我们正在开发的应用程序中,我们使用带有 sqlite 后备存储的 Core Data 来存储我们的数据。我们应用程序的对象模型很复杂。此外,我们的应用程序提供的数据总量太大,无法放入 iOS (iPhone/iPad/iPod Touch) 应用程序包中。由于我们的用户通常只对数据的一个子集感兴趣,因此我们对数据进行了分区,使应用程序附带了数据对象的一个​​子集(尽管约为 100 MB)应用程序包。我们的用户在通过 iTunes 应用程序内购买支付附加内容后,可以选择从我们的服务器下载附加数据对象(大小约为 5 MB 到 100 MB)。 增量数据文件(存在于 sqlite 后备存储中)使用与 bundle 附带的数据相同的 xcdatamodel 版本;对象模型的更改为零。增量数据文件作为 gzipped sqlite 文件从我们的服务器下载。我们不想通过将增量内容与应用程序一起发送来膨胀我们的应用程序包。此外,我们不想依赖 web 服务的查询(因为复杂的数据模型)。 我们已经测试了从我们的服务器下载增量 sqlite 数据。我们已经能够将下载的数据存储添加到应用程序的共享 persistentStoreCoordinator。

{
       NSError *error = nil;
       NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                                [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

       if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:defaultStoreURL options:options error:&error])
       {            
           NSLog(@"Failed with error:  %@", [error localizedDescription]);
           abort();
       }    

       // Check for the existence of incrementalStore
       // Add incrementalStore
       if (incrementalStoreExists) {
           if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:incrementalStoreURL options:options error:&error])
           {            
               NSLog(@"Add of incrementalStore failed with error:  %@", [error localizedDescription]);
               abort();
           }    
       }
 }

但是,这样做有两个问题。

  1. 数据获取结果(例如,使用 NSFetchResultController)显示为 incrementalStoreURL 中的数据附加到 来自 defaultStoreURL 的数据。
  2. 有些对象是重复的。有很多实体 我们数据模型中的只读数据;当我们添加时,这些会被复制 第二个 persistentStore 到 persistentStoreCoordinator。

理想情况下,我们希望 Core Data 将来自两个持久存储的对象图合并为一个(在数据下载时来自两个存储的数据之间没有共享关系)。另外,我们想删除重复的对象。 在网上搜索时,我们看到了一些试图做我们正在做的事情的人提出的问题——比如 this answerthis answer .我们读过 Marcus Zarra's blog on importing large data sets in Core Data .然而,我们所见过的解决方案都没有为我们工作。我们不想手动从增量存储中读取数据并将其保存到默认存储中,因为我们认为这在手机上会非常缓慢且容易出错。有没有更有效的合并方式?

我们已尝试通过如下方式实现手动迁移来解决该问题。但是,我们未能成功实现合并。对于上面提到的答案 1 和 2 建议的解决方案,我们并不是很清楚。 Marcus Zarra 的博客解决了我们在将大型数据集导入 iOS 的项目开始时遇到的一些问题。

{
       NSError *error = nil;
       NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                                [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];        

       NSMigrationManager *migrator = [[NSMigrationManager alloc] initWithSourceModel:__managedObjectModel destinationModel:__managedObjectModel];
       if (![migrator migrateStoreFromURL:stateStoreURL
                                type:NSSQLiteStoreType 
                             options:options 
                    withMappingModel:nil
                    toDestinationURL:destinationStoreURL 
                     destinationType:NSSQLiteStoreType 
                  destinationOptions:nil 
                               error:&error])
       {
           NSLog(@"%@", [error userInfo]);
           abort();
       }
}

答案 1 的作者似乎最终从增量存储中读取了他的数据并保存到默认存储中。也许,我们误解了文章 1 和 2 所建议的解决方案。我们的数据大小可能会阻止我们手动读取增量数据并将其重新插入到默认存储中。我的问题是:从两个 persistentStores(具有相同的 objectModel)中获取对象图以合并到一个 persistentStore 中的最有效方法是什么?

当我们向对象图添加新的实体属性或修改关系时,自动迁移非常有效。是否有一种简单的解决方案可以将相似的数据合并到同一个持久性存储中,该持久性存储具有足够的弹性以在自动迁移完成时停止和恢复?

最佳答案

经过几次尝试,我已经弄清楚如何进行这项工作。秘诀是首先为只读实体创建没有任何数据的增量存储数据。如果不将只读数据留在增量存储之外,这些实体实例将在数据迁移和合并后复制。因此,应该在没有这些只读实体的情况下创建增量存储。默认商店将是唯一拥有它们的商店。

例如,我的数据模型中有实体“Country”和“State”。我的对象图中只需要一个 Country 和 State 实例。我将这些实体保留在增量存储之外,并仅在默认存储中创建它们。我使用 Fetched Properties 将我的主要对象图松散地链接到这些实体。我使用模型中的所有实体实例创建了默认商店。增量存储要么没有只读实体(即在我的例子中是 Country 和 State),要么在数据创建完成后将其删除。

下一步是在应用程序启动期间将增量存储添加到它自己的 persistentStoreCoordinator(与我们要将所有内容迁移到的默认存储的协调器不同)。

最后一步是在增量存储上调用 migratePersistentStore 方法以将其数据合并到主(即默认)存储。急!

以下代码片段说明了我上面提到的最后两个步骤。我执行了这些步骤以使我的设置能够将增量数据合并到主数据存储中以正常工作。

{
    NSError *error = nil;
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:defaultStoreURL options:options error:&error])
    {            
        NSLog(@"Failed with error:  %@", [error localizedDescription]);
        abort();
    }    

    // Check for the existence of incrementalStore
    // Add incrementalStore
    if (incrementalStoreExists) {

        NSPersistentStore *incrementalStore = [_incrementalPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:incrementalStoreURL options:options error:&error];
        if (!incrementalStore)
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }    

        if (![_incrementalPersistentStoreCoordinator migratePersistentStore:incrementalStore
            toURL:_defaultStoreURL
            options:options
            withType:NSSQLiteStoreType
            error:&error]) 
        {
            NSLog(@"%@", [error userInfo]);
            abort();

        }

        // Destroy the store and store coordinator for the incremental store
        [_incrementalPersistentStoreCoordinator removePersistentStore:incrementalStore error:&error];
        incrementalPersistentStoreCoordinator = nil;
        // Should probably delete the URL from file system as well
        //
    }
}

关于objective-c - 合并两个 iOS 核心数据持久存储的有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9970103/

相关文章:

c - 为什么我必须在 Objective-C 中仅在某些时候编写 *myPointerVar?

ios - CloudKit : How to handle account changes with local persistent store?

objective-c - 单元格中的 UITextView CornerRadius

ios - RestKit 0.20.1 如何映射父 ID

iphone - NSFetchedResultsController 不显示来自其他上下文的更新

swift - 具有保留关键字的 CoreData NSManagedObject Swift 子类

java - 如何在 Java 中以编程方式合并 EMF 模型?

git - merge 分支以掌握所有更改?

tfs - 在 TFS 2015 中执行无基础合并是否有其他选择?

ios - 从自定义 UITableViewCell 委托(delegate) TabBar