使用MagicalRecord手动迁移

时间:2014-03-03 23:42:36

标签: core-data migration magicalrecord

我正在实现我的CoreData商店的迁移,在那里我用BOOL属性替换字符串属性:当字符串为“0”时,bool应该为“YES”,而在所有其他情况下bool应该是“没有”。听起来很简单,但我想我仍然需要添加一个映射模型。我在Xcode中添加了它,并实现了createDestinationInstancesForSourceInstance

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping: (NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
   NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];

   NSString *oldValue = [sInstance valueForKey: @"oldString"];
   NSNumber *newValue = @(NO);

   if ([oldValue integerValue] == 0)
      newValue = @(YES);

   [newObject setValue: newValue forKey: @"newBool"];

   [manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping];

   return YES;
}

然而这永远不会被召唤。

由于我正在使用MagicalRecord,我也在使用:

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed: @"storename.sqlite"];

我读到我在创建商店时也需要使用:NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @(YES), NSInferMappingModelAutomaticallyOption: @(NO)};,但我如何在MagicalRecord中使用它?

更新:因此,MR使用MR_autoMigrationOptions来设置迁移选项。有没有办法修改这些以支持手动迁移?

1 个答案:

答案 0 :(得分:1)

要执行手动迁移,您需要使用:

[MagicalRecord setupManuallyMigratingStackWithSQLiteStoreNamed: @"storename.sqlite"];