swift - 如何将字段添加到 Realm 数据库的架构中?

标签 swift ios8 realm realm-migration

我正在寻求向我的 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 上的说明进行操作我已经复制了第一个 block 中的代码并将其放入我的“application(application:didFinishLaunchingWithOptions:)”函数中,该函数位于“AppDelegate.swift”文件中。

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 网站上此 block 正下方的声明指出:
“至少,我们需要做的就是用一个空 block 更新版本,以表明架构已由 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

那我做错了什么?

我不需要在新字段中枚举或预填任何值。我只需要我的计划中的新 Realm 。

最佳答案

我将您的代码与文档中的示例和 sample code 进行了比较它看起来不错。从所有方面来看,应该 正常工作。

我唯一能想到的是,Realm 对象可能在您的应用程序委托(delegate)中配置之前在您的应用程序的其他地方被调用。如果您的应用程序使用 Storyboard并且您在 View Controller viewDidLoad 方法中有任何 Realm 代码,这是可能的,因为它会在您的应用程序委托(delegate)被触发之前加载。如果是这种情况,通常很容易解决,因为您需要做的就是手动设置并在 application(application:didFinishLaunchingWithOptions:)

结束时展示您的 Storyboard

否则,esthepiking 所说的是正确的。您可以在测试期间增加模式版本,或者如果您知道您的模式更改在开发期间会非常不稳定,您也可以在每次应用启动时简单地删除它。

关于swift - 如何将字段添加到 Realm 数据库的架构中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34441796/

相关文章:

swift - 从类方法中查找类的实例

swift - 如何为 iOS 7 和 iOS 8 实现搜索 Controller

realm - 将带有包裹的 Realm 对象传递给事件已返回 null

swift - 为什么 `var one: AnyObject = [AnyObject]()`有效?

swift - 如何使用 SpriteKit AR 在 Swift 中的 SKScene 上创建覆盖 SCNScene?

ios - 为什么这个 iOS 应用程序需要很长时间才能将字符串弹出到 UILabel?

html - 在装有 iOS8 Mobile Safari 和 Safari 8 的 iPad mini 上,在文本输入中打字非常慢

ios8 - 使用 Metal 进行 64 位数字运算

java - JDBC Realm 表单例份验证如何

java - "users"的 Realm IO应用