打开具有特定(非当前)MOM版本的CoreData存储



在CoreData模型版本中,我有各种破坏性的模式更改。由于本地模式只是网络数据的缓存,我很乐意删除sqlite文件,并强制重新下载此版本的iOS应用程序。

然而。。。

数据库中有一个实体是由用户编写的,不应丢失。

在新版本的应用程序中,这些易失性数据不再写入CoreData存储,而是记录在文件系统中。

我正在逐步进行,以便(例如)应用程序的2.0.1版本使用v1模式并复制数据,但没有中断的模式更改,然后应用程序的2.0版本2可以添加模式更改并删除数据库,因为知道数据已经被删除,这是安全的,但由于应用程序商店更新的工作方式,无法保证用户从.0到.1到.2。当他们试图打开核心数据存储时,他们可能会直接从.0到.2,并遇到突破性的更改。

任何想法、建议、建议都将不胜感激。

最新消息:我想知道"假装"移民是否是一种方式。一种自定义迁移策略,它忽略除易失性数据之外的所有内容,并且不实际迁移数据,只将其写入文件系统。一旦运行完毕,我就会在文件系统中拥有易失性数据,并根据新模式创建一个空的DB。

我还认为你可能更擅长伪造迁移,这里有一些代码可以获得你想要的妈妈。只需将EntryModel替换为您的模型名称,然后调整版本即可。然后在NSPersistentStoreCoordinator上使用"-(id)initWithManagedObjectModel"方法获取所需模型上的存储协调器。

- (NSManagedObjectModel *)managedObjectModelForVersion:(NSString*)version {
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"EntryModel" ofType:@"momd"];
if (BETWEEN_INEX(version, @"1.0", @"1.4")) {
modelPath = [modelPath stringByAppendingPathComponent:@"EntryModel"];
modelPath = [modelPath stringByAppendingPathExtension:@"mom"];
} else if (BETWEEN_INEX(version, @"1.4", @"1.5")) {
modelPath = [modelPath stringByAppendingPathComponent:@"EntryModel 2"];
modelPath = [modelPath stringByAppendingPathExtension:@"mom"];
} else if (BETWEEN_INEX(version, @"1.5", @"1.6")) {
modelPath = [modelPath stringByAppendingPathComponent:@"EntryModel 3"];
modelPath = [modelPath stringByAppendingPathExtension:@"mom"];
} else if (BETWEEN_INEX(version, @"1.6", @"1.7")) {
modelPath = [modelPath stringByAppendingPathComponent:@"EntryModel 4"];
modelPath = [modelPath stringByAppendingPathExtension:@"mom"];
}
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
NSManagedObjectModel * oldManagedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSSet *vIdentifiers = [oldManagedObjectModel versionIdentifiers];
for (NSString * identifier in vIdentifiers) {
NSLog(@"Old Model : %@",identifier);
}
return [oldManagedObjectModel autorelease];
}

这也可能有用:

#define GREATER_THAN(w,v)              ([w compare:v options:NSNumericSearch] == NSOrderedDescending)
#define GREATER_THAN_OR_EQUAL_TO(w,v)  ([w compare:v options:NSNumericSearch] != NSOrderedAscending)
#define LESS_THAN(w,v)                 ([w compare:v options:NSNumericSearch] == NSOrderedAscending)
#define LESS_THAN_OR_EQUAL_TO(w,v)     ([w compare:v options:NSNumericSearch] != NSOrderedDescending)
#define BETWEEN_INCLUDE(w,v,z)     (GREATER_THAN_OR_EQUAL_TO(w,v) && LESS_THAN_OR_EQUAL_TO(w,z))
#define BETWEEN_EXCLUDE(w,v,z)     (GREATER_THAN(w,v) && LESS_THAN(w,z))
#define BETWEEN_INEX(w,v,z)     (GREATER_THAN_OR_EQUAL_TO(w,v) && LESS_THAN(w,z))
#define BETWEEN_EXIN(w,v,z)     (GREATER_THAN(w,v) && LESS_THAN_OR_EQUAL_TO(w,z))

最新更新