ios - 删除 Realm 对象中的属性

标签 ios objective-c realm

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

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

Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'Stock' due to the following errors: - Property 'percentageOn' is missing from latest object model.'

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

最佳答案

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

// 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];

关于ios - 删除 Realm 对象中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30730072/

相关文章:

android - Xamarin Realm 没有 realobject 链接器剥离了它们

android - RealmResults 与 RealmList 之间的区别?

ios - 在 Swift 的 NSObject 子类的初始化器中调用 super.init()

objective-c - xcode4 分析显示一行我不确定如何修复

objective-c - NSFileHandleDataAvailableNotification 文件重复无新数据(导致 CPU 使用率非常高)

ios - 如何在 UITextView 中控制/隐藏快捷操作

ios - 未捕获的异常 'NSInvalidArgumentException',原因 : -[_SwiftValue floatValue]: unrecognized selector sent to instance

iphone - 图片发布到 Facebook 最终出现在错误的位置

iphone - Storyboard - 在 Storyboard 中为同一个 ViewController 创建两个不同的 View

android - 如何在 Android 上处理 Realm 事务