我还认为你可能会更好地伪造迁移,这里有一些代码可以让你想要的那个妈妈。只需将 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))