领域迁移,初始化位置
Realm migration, where to initialise
我正在尝试使用一种记录的方法来迁移领域数据库和设置架构版本。我使用的代码类型是:
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
这似乎是非常标准的代码,并且看起来已被其他人使用。然而,似乎让我感到困惑的是我正在初始化 Realm 实例,这导致架构设置无法设置或持续存在。
我正在纠结的是在哪里设置以下代码:
let uiRealm = try! Realm()
- 如果我把它放在 AppDelegate 的顶部 @UIApplicationMain 上面它初始化太早
- 如果我创建一个控制器文件,我打算在迁移后调用其中的一个函数,并将初始化程序放在它的顶部,它仍然不起作用
如果我将它放在 ViewController 的 class 中,如下面的代码,我会收到错误 Instance member uiRealm cannot be used on type XYZViewController
import UIKit
import RealmSwift
class XYZViewController: UITableViewController,UIPickerViewDataSource,UIPickerViewDelegate {
let uiRealm = try! Realm()
var scenarios = uiRealm.objects(Scenario).filter("isActive = true ")
}
所以我的问题是:是否有关于在哪里初始化以及如何最好地迁移的最佳实践。
您需要确保在代码调用 Realm()
.
的任何其他部分之前,您已将 Configuration
对象设置为 Realm 的默认配置
最佳做法是不要保留对 Realm()
的任何引用,除非您有充分的理由。每次调用 Realm()
时,它都会 return 之前缓存的对象实例,因此通过创建对实例的引用,然后在您的生命周期内挂在该实例上,没有任何性能优势应用
使用迁移信息设置 Configuration
对象的最佳位置是尽快,在代码有机会调用 Realm()
之前。所以 app delegate 是一个很好的地方。
如果您有 class 依赖于 Realm()
的属性已经被预配置,在这些属性前加上 lazy
关键字可能会有所帮助,这样它们的创建就会延迟到你真的需要它们。
我正在尝试使用一种记录的方法来迁移领域数据库和设置架构版本。我使用的代码类型是:
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
这似乎是非常标准的代码,并且看起来已被其他人使用。然而,似乎让我感到困惑的是我正在初始化 Realm 实例,这导致架构设置无法设置或持续存在。
我正在纠结的是在哪里设置以下代码:
let uiRealm = try! Realm()
- 如果我把它放在 AppDelegate 的顶部 @UIApplicationMain 上面它初始化太早
- 如果我创建一个控制器文件,我打算在迁移后调用其中的一个函数,并将初始化程序放在它的顶部,它仍然不起作用
如果我将它放在 ViewController 的 class 中,如下面的代码,我会收到错误 Instance member uiRealm cannot be used on type XYZViewController
import UIKit import RealmSwift class XYZViewController: UITableViewController,UIPickerViewDataSource,UIPickerViewDelegate { let uiRealm = try! Realm() var scenarios = uiRealm.objects(Scenario).filter("isActive = true ") }
所以我的问题是:是否有关于在哪里初始化以及如何最好地迁移的最佳实践。
您需要确保在代码调用 Realm()
.
Configuration
对象设置为 Realm 的默认配置
最佳做法是不要保留对 Realm()
的任何引用,除非您有充分的理由。每次调用 Realm()
时,它都会 return 之前缓存的对象实例,因此通过创建对实例的引用,然后在您的生命周期内挂在该实例上,没有任何性能优势应用
使用迁移信息设置 Configuration
对象的最佳位置是尽快,在代码有机会调用 Realm()
之前。所以 app delegate 是一个很好的地方。
如果您有 class 依赖于 Realm()
的属性已经被预配置,在这些属性前加上 lazy
关键字可能会有所帮助,这样它们的创建就会延迟到你真的需要它们。