迁移运行后立即遍历 Realm 中的所有记录 - Swift / iOS
Iterate through all of the records in Realm right after a migration runs - Swift / iOS
我有一个 iOS/Swift 应用程序,我需要在其中更改并向 Realm
对象添加一些新属性,这意味着我将不得不创建一个新架构并更改一些现有数据相应地在 Realm
内,因此它适用于新的更改。
放置我的代码以在应用程序启动后立即重构 Realm
中现有数据的最佳位置在哪里?
编辑:
我知道如何创建迁移以及在哪里进行。
我不确定在迁移代码运行后立即遍历 Realm
中的所有记录以对 Realm
中的现有数据进行一些更改的好地方在哪里].
更具体地说,我需要遍历记录的主要原因是因为我目前正在使用 String
属性 来存储 quantities
如下... 2 pcs
,不要问我为什么,但现在我需要从数量值中删除 pcs
,只留下 2
,以便能够将 属性 更改为 Int
或 Double
。我知道我知道,使用字符串来存储数量是愚蠢的。
在 AppDelegate 的 disFinishLaunchWithOptions
方法中编写迁移代码。
请参考以下参考资料:
本地迁移:
Local migrations are defined by setting
Realm.Configuration.schemaVersion and
Realm.Configuration.migrationBlock. Your migration block provides all
the logic for converting data models from previous schemas to the new
schema. When creating a Realm with this configuration, the migration
block will be applied to update the Realm to the given schema version
if a migration is needed.
Suppose we want to migrate the Person model declared earlier. The
minimal necessary migration block would be the following:
// 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) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// 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()
更新:
realm.io official docs 已提供所有迁移所需的信息。
本地迁移
If you want to add new property only then above code will work for
you. No need to iterate all records. The old record keeps its default
value with the respective row.
更新值
If you want to add a new property by using existing column value. You
need to iterate all the records. Please refer below code.
// Inside your application(application:didFinishLaunchingWithOptions:)
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// The enumerateObjects(ofType:_:) method iterates
// over every Person object stored in the Realm file
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
// combine name fields into a single field
let firstName = oldObject!["firstName"] as! String
let lastName = oldObject!["lastName"] as! String
newObject!["fullName"] = "\(firstName) \(lastName)"
}
}
})
重命名属性
If you just want to rename property only then use below code.
// Inside your application(application:didFinishLaunchingWithOptions:)
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// The renaming operation should be done outside of calls to `enumerateObjects(ofType: _:)`.
migration.renameProperty(onType: Person.className(), from: "yearsSinceBirth", to: "age")
}
})
我有一个 iOS/Swift 应用程序,我需要在其中更改并向 Realm
对象添加一些新属性,这意味着我将不得不创建一个新架构并更改一些现有数据相应地在 Realm
内,因此它适用于新的更改。
放置我的代码以在应用程序启动后立即重构 Realm
中现有数据的最佳位置在哪里?
编辑:
我知道如何创建迁移以及在哪里进行。
我不确定在迁移代码运行后立即遍历 Realm
中的所有记录以对 Realm
中的现有数据进行一些更改的好地方在哪里].
更具体地说,我需要遍历记录的主要原因是因为我目前正在使用 String
属性 来存储 quantities
如下... 2 pcs
,不要问我为什么,但现在我需要从数量值中删除 pcs
,只留下 2
,以便能够将 属性 更改为 Int
或 Double
。我知道我知道,使用字符串来存储数量是愚蠢的。
在 AppDelegate 的 disFinishLaunchWithOptions
方法中编写迁移代码。
请参考以下参考资料:
本地迁移:
Local migrations are defined by setting Realm.Configuration.schemaVersion and Realm.Configuration.migrationBlock. Your migration block provides all the logic for converting data models from previous schemas to the new schema. When creating a Realm with this configuration, the migration block will be applied to update the Realm to the given schema version if a migration is needed.
Suppose we want to migrate the Person model declared earlier. The minimal necessary migration block would be the following:
// 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) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// 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()
更新:
realm.io official docs 已提供所有迁移所需的信息。
本地迁移
If you want to add new property only then above code will work for you. No need to iterate all records. The old record keeps its default value with the respective row.
更新值
If you want to add a new property by using existing column value. You need to iterate all the records. Please refer below code.
// Inside your application(application:didFinishLaunchingWithOptions:)
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// The enumerateObjects(ofType:_:) method iterates
// over every Person object stored in the Realm file
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
// combine name fields into a single field
let firstName = oldObject!["firstName"] as! String
let lastName = oldObject!["lastName"] as! String
newObject!["fullName"] = "\(firstName) \(lastName)"
}
}
})
重命名属性
If you just want to rename property only then use below code.
// Inside your application(application:didFinishLaunchingWithOptions:)
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// The renaming operation should be done outside of calls to `enumerateObjects(ofType: _:)`.
migration.renameProperty(onType: Person.className(), from: "yearsSinceBirth", to: "age")
}
})