ios - Realm 数据库迁移,添加新对象并修改旧对象

标签 ios swift3 realm database-migration

我有一个类中定义的 Realm 数据库对象

class TPDailyRashifal: Object, Mappable {

    public required convenience init?(map: Map) {
      self.init()
      mapping(map: map)
    }

    dynamic var rashi: String = ""
    dynamic var rashiDetail: String = ""

    public func mapping(map: Map) {
      rashi       <- map["rashi"]
      rashiDetail <- map["rashifal"]
    }
    override static func primaryKey() -> String {
      return "rashi"
    }
}

我想在我的对象中添加这三个变量,如下所示

dynamic var date: String = ""
dynamic var fallIds: String = ""
dynamic var rating: Int = 0

我知道我的映射函数必须修改并添加以下内容。

    date        <- map["date"]
    fallIds     <- map["fallIds"]
    rating      <- map["rating"]

但是我的

dynamic var rashi: String = ""

定义必须更改为

dynamic var rashi:  Int = 0

在我的AppdelegateapplicationDidFinishLaunchingWithOptions函数中,我写了

    Realm.Configuration.defaultConfiguration = Realm.Configuration(
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                print("Schema Version 0")
                // The enumerateObjects(ofType:_:) method iterates
                // over every Person object stored in the Realm file
                migration.enumerateObjects(ofType: TPDailyRashifal.className()) { oldObject, newObject in
                    // combine name fields into a single field
                    /*
                     To add these variables during migration
                     dynamic var date: String = ""
                     dynamic var fallIds: String = ""
                     dynamic var rating: Int = 0
                     */
                    let oldRashi = oldObject?["id"] as? Int
                 //   let newRashiId = 


                }
            }
    })

我对应该从哪里开始在 Realm 对象中添加新变量感到困惑。我已经提到了其他问题,但我无法跟上它们。

最佳答案

Smarcat 部分正确,但后来我不得不做一些研究,就在这里。

    Realm.Configuration.defaultConfiguration = Realm.Configuration(
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                print("Schema Version 0")
                // The enumerateObjects(ofType:_:) method iterates
                // over every Person object stored in the Realm file
                migration.enumerateObjects(ofType: TPDailyRashifal.className()) { oldObject, newObject in
                    // combine name fields into a single field

                    newObject!["date"] = ""
                    newObject!["fallIds"] = ""
                    newObject!["rating"] = 0
                      newObject!["rashi2"] = 0
                }
                migration.renameProperty(onType: TPDailyRashifal.className(), from: "rashi", to: "rashi2")

            }
    })

我必须使用 migration.renameProperty block 将 rashi 重命名为 rashi2

关于ios - Realm 数据库迁移,添加新对象并修改旧对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45830947/

相关文章:

swift - 在 Cocoa Touch 框架中使用 Realm 时的模式

ios - 在 Swift 中禁用和灰显 UITextField

iOS NSFileManager 使用 URL 而不是路径将文件存储在文档目录中?

ios - CocoaPods 和 Swift 3.0

swift - 以编程方式选择 tableview 中的所有单元格,以便下次按下它们时调用 didDeselect

ios - 为什么Realm对象中没有我的属性?

ios - 我可以在 iOS 中自定义推送通知的外观吗?

ios - UIView组件已加载但未显示

ios - 合并目录中的多个视频

swift - 使用 LInkingOjbects 的 Realm 在运行时出现 fatal error