ios - 如何更改类的属性而不必使用 Realm 删除应用程序

标签 ios swift realm persistent-storage

目前我正在使用 realm 在 Swift 中编写程序。我是 iOS 开发的新手,但我对 Realm 的理解是,当您更改存储在 Realm 中的类时,您需要从设备中删除应用程序以删除持久数据。不幸的是,我已经在应用程序中手动输入了一个相当大的数据库。

目前我需要更改类内的属性名称,但将来可能需要添加属性。更新 Realm 存储的最佳方式是什么,这样我就不需要删除应用程序?

这是我的一个模型:

class Device: Object {

   dynamic var name = ""
   dynamic var id = ""
   dynamic var os = ""
   dynamic var currentUser: User?
   dynamic var dateStamp = NSDate()
}

最佳答案

您可以添加迁移,如 our docs 中所示并使用它来将旧值接管到新属性中:

objective-C

// Inside your [AppDelegate didFinishLaunchingWithOptions:]

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
config.schemaVersion = 1;

// Set the block which will be called automatically when opening a Realm with a
// schema version lower than the one set above
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
        // The -enumerateObjects:block: method iterates
        // over every Device object stored in the Realm file
        [migration enumerateObjects:Device.className
                      block:^(RLMObject *oldObject, RLMObject *newObject) {
            // e.g. Rename 'os' to 'operatingSystem'
            newObject[@"operatingSystem"] = oldObject[@"os"]
        }];
    }
};

// Tell Realm to use this new configuration object for the default Realm
[RLMRealmConfiguration setDefaultConfiguration:config];

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
[RLMRealm defaultRealm];

Swift(使用 Realm Swift)

https://realm.io/docs/swift/latest/#performing-a-migration

// Inside your application(application:didFinishLaunchingWithOptions:)

let config = Realm.Configuration(
  // Set the new schema version. This must be greater than the previously used
  // version (if you've never set a schema version before, the version is 0).
  schemaVersion: 1,

  // Set the block which will be called automatically when opening a Realm with
  // a schema version lower than the one set above
  migrationBlock: { migration, oldSchemaVersion in
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
        // The enumerate(_:_:) method iterates
        // over every Device object stored in the Realm file
        migration.enumerate(Device.className()) { oldObject, newObject in
            // e.g. Rename 'os' to 'operatingSystem'
            newObject["operatingSystem"] = oldObject["os"]
        }
    }
  })

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()

关于ios - 如何更改类的属性而不必使用 Realm 删除应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35658916/

相关文章:

ios - 如何遍历字典?

ios - 在第一个 Controller 发生事件时以编程方式返回到第一个 Controller

ios - 为什么必须在主队列上异步调用 resignFirstResponder() 来关闭键盘

swift - Swift 中的注释会减慢编译时间吗?

android - 注解处理时从/src和/build中扫描源文件

java - 安卓 Realm : compactRealm not working as expected

ios - 如何将Point2f转换为UIImage的位置?

ios - 如何在HERE MAP iOS SDK中获得特定国家/地区的结果(仅适用于英国)?

javascript - 错误 : Realm context not found. 您是否在 <RealmProvider/> 中调用了 useRealm()?

android - PageStorageKey 的 Flutter ListView 性能问题