领域迁移问题
Realm migration issue
对于加载看似重复的问题,我深表歉意。
我是第一次尝试 Realm - 我创建了一个测试项目来完成它。我向我的模型 class 添加了另一个 属性,然后收到有关需要更改和迁移的错误消息。我按照说明在 AppDelegate 中设置了一些用于迁移的代码(尽管迁移块是空的,因为我删除了所有记录)但是当它再次启动后到达我的 ViewController 时它崩溃了 "Provided schema version 0 is less than last set version 1" 并且我就是过不去?
它在我的 ViewController 上失败了 var realm = 试试!境界()
我错过了什么?
你似乎做对了。根据 realm documentation:
At the very minimum all we need to do is to update the version with an empty block to indicate that the schema has been upgraded (automatically) by Realm.
我的猜测是您正在创建配置和迁移,但没有将其设置为默认领域配置,或者设置配置太晚(在实例化 realm ).
根据您遇到的错误
Provided schema version 0 is less than last set version 1
似乎根本没有执行迁移。在任何情况下,每次更新架构时,您还应该在 领域配置 中增加 schemaVersion
。该错误意味着您的配置包含的版本 (0
) 低于磁盘中现有数据库的版本 (1
)。在这种情况下,您的新配置应具有至少 2
的 schemaVersion
(高于磁盘版本的任何值)。
在实例化 领域 之前,如在您的应用委托 application:didFinishLaunchingWithOptions:
中,根据您当前的 schemaVersion
,您至少需要如下内容。
let config = Realm.Configuration(
schemaVersion: 2, // Must be greater than previous version
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// minimally this can be empty
}
if (oldSchemaVersion < 2) {
// minimally this can be empty
}
print("Realm migration did run") // Log to know migration was executed
})
// Make sure to set the default configuration
Realm.Configuration.defaultConfiguration = config
我还建议您在实例化 realm 之前,在迁移块和视图控制器中进行一些日志记录或设置断点。这样您就可以知道是否曾经执行过迁移。
但是,由于您似乎只是在学习 realm,我建议您暂时忽略迁移的细节。为避免这种情况,您可以在更改数据库架构时从 device/simulator 中卸载您的应用程序。只需确保将 schemaVersion
用作 0
,您将始终使用全新的数据库,因此不需要迁移。
第二种选择是使用 in-memory realms。这些不会保存到磁盘,因此数据不会在应用程序启动时持续存在,但它仍然像普通的 realm 数据库一样工作。这非常适合早期原型制作。要获得其中之一,您只需为您的配置提供 inMemoryIdentifier
.
let config = Realm.Configuration(inMemoryIdentifier: "ThisRealmIsNotStored")
Realm.Configuration.defaultConfiguration = config
对于加载看似重复的问题,我深表歉意。
我是第一次尝试 Realm - 我创建了一个测试项目来完成它。我向我的模型 class 添加了另一个 属性,然后收到有关需要更改和迁移的错误消息。我按照说明在 AppDelegate 中设置了一些用于迁移的代码(尽管迁移块是空的,因为我删除了所有记录)但是当它再次启动后到达我的 ViewController 时它崩溃了 "Provided schema version 0 is less than last set version 1" 并且我就是过不去?
它在我的 ViewController 上失败了 var realm = 试试!境界()
我错过了什么?
你似乎做对了。根据 realm documentation:
At the very minimum all we need to do is to update the version with an empty block to indicate that the schema has been upgraded (automatically) by Realm.
我的猜测是您正在创建配置和迁移,但没有将其设置为默认领域配置,或者设置配置太晚(在实例化 realm ).
根据您遇到的错误
Provided schema version 0 is less than last set version 1
似乎根本没有执行迁移。在任何情况下,每次更新架构时,您还应该在 领域配置 中增加 schemaVersion
。该错误意味着您的配置包含的版本 (0
) 低于磁盘中现有数据库的版本 (1
)。在这种情况下,您的新配置应具有至少 2
的 schemaVersion
(高于磁盘版本的任何值)。
在实例化 领域 之前,如在您的应用委托 application:didFinishLaunchingWithOptions:
中,根据您当前的 schemaVersion
,您至少需要如下内容。
let config = Realm.Configuration(
schemaVersion: 2, // Must be greater than previous version
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// minimally this can be empty
}
if (oldSchemaVersion < 2) {
// minimally this can be empty
}
print("Realm migration did run") // Log to know migration was executed
})
// Make sure to set the default configuration
Realm.Configuration.defaultConfiguration = config
我还建议您在实例化 realm 之前,在迁移块和视图控制器中进行一些日志记录或设置断点。这样您就可以知道是否曾经执行过迁移。
但是,由于您似乎只是在学习 realm,我建议您暂时忽略迁移的细节。为避免这种情况,您可以在更改数据库架构时从 device/simulator 中卸载您的应用程序。只需确保将 schemaVersion
用作 0
,您将始终使用全新的数据库,因此不需要迁移。
第二种选择是使用 in-memory realms。这些不会保存到磁盘,因此数据不会在应用程序启动时持续存在,但它仍然像普通的 realm 数据库一样工作。这非常适合早期原型制作。要获得其中之一,您只需为您的配置提供 inMemoryIdentifier
.
let config = Realm.Configuration(inMemoryIdentifier: "ThisRealmIsNotStored")
Realm.Configuration.defaultConfiguration = config