如何将字段添加到 Realm 数据库的架构中?
How to add a field to the schema of a Realm database?
我正在寻求向我的 Realm 数据库方案(箭头所指)添加另一个 属性,同时学习如何使用迁移功能。
class FeesPaid: Object {
dynamic var fileNumber = ""
dynamic var forMonth = ""
dynamic var amount: Float = 0.0
dynamic var balance: Float = 0.0 <-------
dynamic var date = ""
}
我一直在按照 Realm.io 中的说明进行操作,我已经复制了第一块中的代码并将其放入位于 "AppDelegate.swift" 文件中的我的 "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 clients = try! Realm()
Realm 网站上此块正下方的声明指出:
"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."
(上面的 if 语句中的注释似乎支持这种不需要做任何其他事情的声明。)
然而,当我 运行 我的应用程序时,我不断收到错误消息:
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Migration is required for object type 'FeesPaid' due to the following errors:
- Property 'balance' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required for object type 'FeesPaid' due to the following errors:
- Property 'balance' has been added to latest object model.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 50
那我做错了什么?
我不需要在新字段中枚举或预填任何值。我只需要我的计划中的新领域。
我将您的代码与文档和 sample code 中的示例进行了比较,看起来不错。所有人都认为 应该 有效。
我唯一能想到的是 Realm
对象可能在您的应用程序委托中配置之前在您的应用程序的其他地方被调用。如果您的应用程序使用故事板并且您在视图控制器 viewDidLoad
方法中有任何 Realm 代码,这是可能的,因为它会在您的应用程序委托被触发之前加载。如果是这种情况,通常很容易解决,因为您需要做的就是手动设置并在 application(application:didFinishLaunchingWithOptions:)
结束时展示您的故事板
否则,esthepiking 所说的是正确的。您可以在测试期间增加模式版本,或者如果您知道您的模式更改在开发期间会非常不稳定,您也可以在每次应用启动时简单地删除它。
我正在寻求向我的 Realm 数据库方案(箭头所指)添加另一个 属性,同时学习如何使用迁移功能。
class FeesPaid: Object {
dynamic var fileNumber = ""
dynamic var forMonth = ""
dynamic var amount: Float = 0.0
dynamic var balance: Float = 0.0 <-------
dynamic var date = ""
}
我一直在按照 Realm.io 中的说明进行操作,我已经复制了第一块中的代码并将其放入位于 "AppDelegate.swift" 文件中的我的 "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 clients = try! Realm()
Realm 网站上此块正下方的声明指出:
"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."
(上面的 if 语句中的注释似乎支持这种不需要做任何其他事情的声明。)
然而,当我 运行 我的应用程序时,我不断收到错误消息:
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Migration is required for object type 'FeesPaid' due to the following errors: - Property 'balance' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required for object type 'FeesPaid' due to the following errors: - Property 'balance' has been added to latest object model.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 50
那我做错了什么?
我不需要在新字段中枚举或预填任何值。我只需要我的计划中的新领域。
我将您的代码与文档和 sample code 中的示例进行了比较,看起来不错。所有人都认为 应该 有效。
我唯一能想到的是 Realm
对象可能在您的应用程序委托中配置之前在您的应用程序的其他地方被调用。如果您的应用程序使用故事板并且您在视图控制器 viewDidLoad
方法中有任何 Realm 代码,这是可能的,因为它会在您的应用程序委托被触发之前加载。如果是这种情况,通常很容易解决,因为您需要做的就是手动设置并在 application(application:didFinishLaunchingWithOptions:)
否则,esthepiking 所说的是正确的。您可以在测试期间增加模式版本,或者如果您知道您的模式更改在开发期间会非常不稳定,您也可以在每次应用启动时简单地删除它。