首页 文章

删除Realm对象中的属性

提问于
浏览
4

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

我刚刚从我的对象的头文件中删除了该属性,但是这不起作用,因为我收到此错误:

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

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

1 回答

  • 5

    大卫说的是对的 . 如果确保正确执行迁移,则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];
    

相关问题