【问题标题】:deleting core data persistent store instead of migrating (also using RestKit)删除核心数据持久存储而不是迁移(也使用 RestKit)
【发布时间】:2012-03-24 11:26:21
【问题描述】:

我正在对我们的应用升级版本的对象模型进行相当多的更改,即。添加/删除的实体,新的属性和关系。看起来这项工作真的会增加适当的核心数据迁移。由于数据主要用作缓存以增强离线浏览体验。在这一点上真的需要迁移我认为如果它只是被吹走并重新创建会简单得多。

根据我遇到的有关该主题的各种帖子,一般策略是

  • 检测模型已更改(通过在 managedObjectContext 的初始化)
  • 删除持久存储(在我们的 iOS 案例中是 sqlite 文件)
  • 使用最新模式重新初始化 objectModel 重新初始化持久存储 使用新模型

这是重新初始化 objectModel 的代码

- (NSManagedObjectModel *)managedObjectModel {

if (managedObjectModel != nil) {
    return managedObjectModel;
}

NSString *path = [[NSBundle mainBundle] pathForResource:@"<model name>" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

return managedObjectModel; 
}

并使用

重新创建 objectModel 和存储
objectManager = [RKObjectManager objectManagerWithBaseURL:
                     [NSString stringWithFormat:@"http://%@/v3", 
                      [[NSBundle mainBundle] objectForInfoDictionaryKey:@"APIDomain"]]];     
NSManagedObjectModel *objectModel = [self managedObjectModel];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:storeName usingSeedDatabaseName:nil managedObjectModel:objectModel delegate:nil]; 

但是,我收到以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“UTCity”的 NSManagedObjectModel”

我觉得已经很接近了,因为重启应用成功创建新商店运行正常。

-PF

【问题讨论】:

    标签: ios core-data restkit


    【解决方案1】:

    我想我已经能够通过实现RKManagedObjectStoreDelegate 中的方法来完成您所描述的操作。当持久存储创建失败时调用该方法。调用此方法时,我只是删除了持久存储。 RestKit 似乎从这一切恢复得很好。我假设它在下次需要它时创建了新的空白商店。

    - (void)managedObjectStore:(RKManagedObjectStore *)objectStore didFailToCreatePersistentStoreCoordinatorWithError:(NSError *)error {
        [objectStore deletePersistentStore];
    }
    

    RKManagedObjectStore 尝试在初始化时创建持久存储,因此您需要通过接受委托对象的方法之一来初始化您的 RKManagedObjectStore 实例。我刚刚传入了我的应用委托。

    到目前为止,这似乎有效。随着我继续发展,我们会看看它是否会继续这样做。

    【讨论】:

    • 看起来 -[RKManagedObjectStore resetPersistentStores:] 可能是 RestKit 0.20.x 中的等价物。
    • 这个委托函数 managedObjectStore:didFailToCreatePersistentStoreCoordinatorWithError: 似乎在 0.20 中消失了。你知道替代品吗?
    【解决方案2】:

    这是一个在迁移失败时完全删除持久存储的解决方案。

        // Core Data Persistent Store
        NSError *error;
        NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Data.sqlite"];
        NSPersistentStore __unused *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath
                                                                                  fromSeedDatabaseAtPath:nil
                                                                                       withConfiguration:nil
                                                                                                 options:@{NSInferMappingModelAutomaticallyOption: @YES, NSMigratePersistentStoresAutomaticallyOption: @YES}
                                                                                                   error:&error];
    
        // Reset the persistant store when the data model changes
        if (error) {
    
            [[NSFileManager defaultManager] removeItemAtPath:storePath
                                                       error:nil];
    
            NSPersistentStore __unused *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath
                                                                                      fromSeedDatabaseAtPath:nil
                                                                                           withConfiguration:nil
                                                                                                     options:nil
                                                                                                       error:nil];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多