【问题标题】:Manual migration with MagicalRecord使用 MagicalRecord 手动迁移
【发布时间】:2014-03-03 23:42:35
【问题描述】:

我正在实现我的 CoreData 存储的迁移,其中我将字符串属性替换为 BOOL 属性:当字符串为“0”时,布尔值应为“YES”,在所有其他情况下,布尔值应为“不”。听起来很简单,但我想我仍然需要添加一个映射模型。我在 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 来设置迁移选项。有没有办法修改这些以支持手动迁移?

【问题讨论】:

    标签: core-data migration magicalrecord


    【解决方案1】:

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

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

    【讨论】:

    • MR里没有那个,你用的是哪个版本的?
    • 这是一个未发布的版本,我正在犹豫是否要使用它。不过还是谢谢。
    • 物有所值,我已经使用它几个月了,它一直很稳定。话虽如此,我还没有尝试过手动迁移堆栈。
    • 在 Tim Roadley 的一本关于 iOS 版 CoreData 的优秀新书的推动下,我实际上在这个周末(在你今天回答之前)做出了暂时不使用 MR 的决定。我强迫自己通过直接与核心数据交互而不是通过框架来更好地理解核心数据。一旦我更好地理解它,我可能会回到MR。再次感谢您的洞察力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2013-05-26
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多