Reputation: 2072
I have a realm database Object as defined in the class
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"
}
}
I would like to add these three variables in my Object as follows
dynamic var date: String = ""
dynamic var fallIds: String = ""
dynamic var rating: Int = 0
I know my mapping function would have to be modified and to add followings.
date <- map["date"]
fallIds <- map["fallIds"]
rating <- map["rating"]
But my
dynamic var rashi: String = ""
definition has to change to
dynamic var rashi: Int = 0
In my Appdelegate
, applicationDidFinishLaunchingWithOptions
function, I have written
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 =
}
}
})
I am confused to where should I begin adding the new variables in the Realm Object. I have referred to other questions, but I couldn't catch up with them.
Upvotes: 0
Views: 1317
Reputation: 2072
Smarcat
was partially right but then I had to do some research and here it is.
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")
}
})
I had to use migration.renameProperty
block to rename rashi
to rashi2
.
Upvotes: 2
Reputation: 2882
I do believe you'll need to rename your "rashi" to something else (assuming the new Int will be "rashi2" in below example).
migration.enumerateObjects(ofType: TPDailyRashifal.className()) { oldObject, newObject in
newObject!["date"] = ""
newObject!["fallIds"] = ""
newObject!["rating"] = 0
newObject!["rashi2"] = 0 // this version has a rashi2 (Int) instead of a rashi (String).
}
Also note, if you need to convert the old rashi to the rashi2 via a string-to-int conversion, you can do so by using oldObject["rashi"] within this closure to obtain the old string, then convert it and set rashi2 to it rather than to 0.
Upvotes: 0