我正在尝试删除我的一个 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/