Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound key issue Realm Swift

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?

like image 659
amagain Avatar asked Mar 04 '17 06:03

amagain


1 Answers

I didn't use Alamofire, so I assumed your code on Alamofire part was correct. You didn't give the structure of your JSON, basing on the context, I assumed your JSON contained 31 dictionaries. Also, I assumed at the very beginning, the Realm database was empty. If not, please make it empty.

I believe the problem is here.

for (_, value): (String, JSON) in json {
    let tpTodayOb = Mapper<TPToday>().map(JSONObject: value.dictionaryObject)

    realm.add(tpTodayOb!, update: true)
}

Please change it to

for (_, value): (String, JSON) in json {
    let tpTodayOb = Mapper<TPToday>().map(JSONObject: value.dictionaryObject)

    realm.add(tpTodayOb!, update: false) // you don't need `update:true`, unless you want to rewrite it intendedly
}

and run your project. If Realm throws duplicated id error, it must be that your compoundKeys are not successfully changed after initialization. Then you should check on that part. Maybe you should call it manually, or override the corresponding part of your init function.

for (_, value): (String, JSON) in json {
    let tpTodayOb = Mapper<TPToday>().map(JSONObject: value.dictionaryObject)
    tpTodayOb.setCompoundID(yearNp: Int, mahina: String, gate: Int)
    realm.add(tpTodayOb!, update: false)
}
like image 80
Owen Zhao Avatar answered Oct 22 '22 01:10

Owen Zhao