删除Realm对象中的属性

时间:2015-06-09 10:57:42

标签: ios objective-c realm

我正在尝试删除其中一个Realm对象中的属性,但是我不确定如何为此编写迁移。

我刚刚从对象的头文件中删除了该属性,但是由于我收到此错误而无效:

  

由于未捕获的异常“RLMException”而终止应用,原因:   由于以下原因,对象类型'Stock'需要迁移   错误:    - 最新对象模型中缺少属性'percentageOn'。'

我知道如何编写迁移添加字段,但如何删除?

1 个答案:

答案 0 :(得分:6)

大卫说的是对的。如果确保正确执行迁移,则Realm可以轻松处理已删除和添加的属性。除非您确实仍然需要percentageOn中的值,否则您甚至可以将迁移块留空,就像Realm网站上的示例一样:

// Inside your [AppDelegate didFinishLaunchingWithOptions:]

// Notice setSchemaVersion is set to 1, this is always set manually. It must be
// higher than the previous version (oldSchemaVersion) or an RLMException is thrown
[RLMRealm setSchemaVersion:1
            forRealmAtPath:[RLMRealm defaultRealmPath] 
        withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {
  // We haven’t migrated anything yet, so oldSchemaVersion == 0
  if (oldSchemaVersion < 1) {
    // Nothing to do!
    // Realm will automatically detect new properties and removed properties
    // And will update the schema on disk automatically
  }
}];

// now that we have called `setSchemaVersion:withMigrationBlock:`, opening an outdated
// Realm will automatically perform the migration and opening the Realm will succeed
[RLMRealm defaultRealm];