I am trying to store json
object to realm
object using Objectmapper
after I receive response from Alamofire
. Below is the code I have written:
func getTodayData() {
Alamofire.request("https://myapipoint.json").responseJSON{ (response) in
guard response.result.isSuccess, let value = response.result.value else {
return
}
let json = JSON(value)
guard let realm = try? Realm() else {
return
}
realm.beginWrite()
for (_, value): (String, JSON) in json {
let tpTodayOb = Mapper<TPToday>().map(JSONObject: value.dictionaryObject)
realm.add(tpTodayOb!, update: true)
}
do {
try realm.commitWrite()
}
catch {
print("Error")
}
}
}
I am able to map json
data from my server. However, there is an issue with my compound key. The three variables are not unique, but their combination is unique, so I had to use compoundKey
as my primary key. I am building primaryKey
from compoundKey
as follows:
public dynamic var compoundKey: String = "0-"
public override static func primaryKey() -> String? {
// compoundKey = self.compoundKeyValue()
return "compoundKey"
}
private func compoundKeyValue() -> String {
return "\(yearNp)-\(mahina)-\(gate)"
}
This is where I have initialized my three variables.
func setCompoundID(yearNp: Int, mahina: String, gate: Int) {
self.yearNp = yearNp
self.mahina = mahina
self.gate = gate
compoundKey = compoundKeyValue()
}
And the definition of compoundKey
as per Github issues is here. I have 31 dictionaries to be stored in my database, but I am only able to store the last dictionary. I'm sure that this is a compound key issue as this codebase is able to store data in another table which has unique field as primary keys, which is not the case in this database table. Have I declared my compoundKey
wrong?