swift - 无法在 Swift 中进行领域迁移
问题描述
我有一个提醒对象,我刚刚修改过。这是原始版本:
class Reminder: Object {
@objc dynamic var title = ""
@objc dynamic var parents = ""
@objc dynamic var lists = "All"
@objc dynamic var labels = "All"
@objc dynamic var priority =
@objc dynamic var notes = ""
@objc dynamic var reminderType = .none
}
这是新版本:
class Reminder: Object {
@objc dynamic var title = ""
@objc dynamic var parents = ""
@objc dynamic var lists = "All"
@objc dynamic var dueDate = 0.0
@objc dynamic var reminderDate = 0.0
@objc dynamic var reminderLocation = ""
@objc dynamic var labels = "All"
@objc dynamic var priority = 1
@objc dynamic var notes = ""
}
我已经实现了didFinishLaunchingWithOptions
AppDelegate 的迁移块方法。这里是:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
///Realm migration
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: 2,
// 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
if oldSchemaVersion < 2 {
}
}
)
Realm.Configuration.defaultConfiguration = config
let _ = try! Realm()
return true
}
根据文档,我认为应该是功能迁移。但是,在编译应用程序时出现以下错误:
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors:
- Property 'Reminder.reminderLocation' has been added.
- Property 'Reminder.reminderDate' has been added.
- Property 'Reminder.dueDate' has been added.
- Property 'Reminder.reminderType' has been removed."
我应该在迁移块中进行哪些更改?
先感谢您
解决方案
当存储的数据与代码中的模型不匹配时,将引发此异常。
您不需要在迁移块中执行任何操作,但是您需要通过更新 的值来触发迁移Realm.Configuration.schemaVersion
,例如:
schemaVersion: 3,
推荐阅读
- c++ - 如何使用自定义类的向量作为冒泡排序算法的输入
- apache-spark - 从 C#/api 编排 Azure 突触火花笔记本
- typescript - 当电子 ABI 与已安装节点不同时,无法生成 TypeORM 迁移
- java - 如何使用 sqlite 获取最后一次磁头的长度
- javascript - 常规 JS 中的 TypeScript 检查和声明
- powershell - 如何使用 Powershell 每 15 分钟重新启动一个批处理文件
- vim - 替换 VIM 中每一行出现的第 N 个字符
- react-native - React Native 应用程序在使用 reducer 时崩溃:“渲染错误;无效的钩子调用。”
- python - 在 python 内存中加载 100 个总大小为 12GB 的 CSV 文件比我的代码快
- python - 将数据从 Excel 导入到特殊的元组列表 - Python