2012-03-06 95 views
7

我正在对我们的应用升级版本即对象模型进行了不少更改。添加/删除的实体,新的属性和关系。看起来这项工作真的可以加入适当的核心数据迁移。由于数据主要用作缓存来增强脱机浏览体验。在这一点上没有真正需要迁移我认为这将是一个更简单的如果它只是吹走和重新创建。删除核心数据持久性存储而不是迁移(也使用RestKit)

基于我遇到关于这一主题的一般策略是

  • 各个岗位检测(在 初始化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]; 

但是存储代码,我收到以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'UTCity''

我觉得它非常接近,因为重新启动应用程序成功地创建一个新的商店运行正确。

-PF

+0

有没有这样的运气? – CharlieMezak 2012-07-22 20:07:55

回答

6

我想我已经能够做你从RKManagedObjectStoreDelegate实施的方法描述什么。该方法在持久性存储创建失败时调用。当调用此方法时,我只是删除持久性存储。 RestKit似乎从此恢复正常。我假设它在下次需要时创建新的空白商店。

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

RKManagedObjectStore尝试创建持久性存储在初始化,所以你需要通过的一个接受委托对象的方法之一来初始化的RKManagedObjectStore实例。我刚刚通过我的应用程序委托。

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

+0

看起来像' - [RKManagedObjectStore resetPersistentStores:]'在RestKit 0.20.x中可能是等效的。 – 2013-06-20 21:03:51

+1

此代理函数managedObjectStore:didFailToCreatePersistentStoreCoordinatorWithError:似乎在0.20中消失。你知道更换吗? – mosca1337 2014-02-28 00:41:03

3

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

// 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]; 
    } 
相关问题