如何更改 class 的属性而无需使用领域删除应用程序
How to change attributes of a class without having to delete the app using realm
目前我正在 Swift 使用领域编写程序。我是 iOS 开发的新手,但我对领域的理解是,当您更改存储在领域中的 class 时,您需要从设备中删除应用程序以删除持久数据。不幸的是,我在应用程序中手动输入了一个相当大的数据库。
目前我需要更改class中的属性名称,但将来可能需要添加属性。更新领域存储的最佳方式是什么,这样我就不需要删除应用程序?
这是我的模型之一:
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(使用领域 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()
目前我正在 Swift 使用领域编写程序。我是 iOS 开发的新手,但我对领域的理解是,当您更改存储在领域中的 class 时,您需要从设备中删除应用程序以删除持久数据。不幸的是,我在应用程序中手动输入了一个相当大的数据库。
目前我需要更改class中的属性名称,但将来可能需要添加属性。更新领域存储的最佳方式是什么,这样我就不需要删除应用程序?
这是我的模型之一:
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(使用领域 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()