我正在对我们的应用升级版本的对象模型进行相当多的更改,即。添加/删除实体,新属性和关系。看起来这项工作真的会为正确的核心数据迁移做好准备。由于数据主要用作缓存以增强离线浏览体验。在这一点上并没有真正需要迁移我觉得如果它被吹走并重新创建将会更加简单。
根据我在这个主题上遇到的各种帖子,一般策略是
这是重新初始化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
答案 0 :(得分:6)
我认为我已经能够通过实施RKManagedObjectStoreDelegate
中的方法来完成您所描述的内容。当持久性存储创建失败时调用该方法。我只是在调用此方法时删除持久存储。 RestKit似乎从这里恢复得很好。我假设它在下次需要时创建了新的空白商店。
- (void)managedObjectStore:(RKManagedObjectStore *)objectStore didFailToCreatePersistentStoreCoordinatorWithError:(NSError *)error {
[objectStore deletePersistentStore];
}
RKManagedObjectStore
尝试在初始化时创建持久性存储,因此您需要通过接受委托对象的方法之一初始化RKManagedObjectStore
的实例。我刚刚通过我的app委托。
到目前为止,这似乎有效。随着我的不断发展,我们将会看到它是否会继续发展。
答案 1 :(得分: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];
}