2

I am using the alamofire to get response JSON from server, then using ObjectMapper to map string to Realm object.

    The realm object is:
    class SolutionVideo: Object, Mappable {

dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""

override static func primaryKey() -> String? {
    return "svID"
}

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

func mapping(map: Map) {
    svID <- map["svID"]
    solutionTitle <- map["solutionTitle"]
    videoName <- map["videoName"]
    relatedInfo <- map["relatedInfo"]
    shortDesc <- map["shortDescription"]
}

}

    The json string is:
    [
    {
        "svID": "10",
        "solutionTitle": "Video10",
        "videoName": "Video10",
        "realtedInfo": "",
        "shortDescription": ""
    },
    {
        "svID": "9",
        "solutionTitle": "Video9",
        "videoName": "Video9",
        "realtedInfo": "",
        "shortDescription": ""
    }
     ]



     in my viewController:
         @IBAction func updateBtn(sender: AnyObject) {
    // download file to update Realm
    let url = "http://janicedemo.com/updates.json"
    Alamofire.request(.GET, url).responseArray { (response: Response<[SolutionVideo], NSError>) in
        let Array = response.result.value
        print(Array)
        if let Array = Array {
            for video in Array {
                let dbURL = Realm.Configuration.defaultConfiguration.fileURL
                let realm = try! Realm(fileURL: dbURL!)
                try! realm.write{
                    print("will save")
                    realm.add(video, update: true)
                }
            }
        }
    }

The problem is that I can add the object successfully. But the svID(primark key) keeps 0, instead of 10 or 9 (set in JSON). Is it because I have set default value to svID? Can someone gives me a hint? Thanks

Janice Zhan
  • 571
  • 1
  • 5
  • 18

2 Answers2

1

Try

class SolutionVideo: Object, Mappable {

dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""

func setCompoundID(id: Int) {
    self.svID = svID
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.solutionTitle = solutionTitle
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.videoName = videoName
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.relatedInfo = relatedInfo
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.shortDesc = shortDesc
    compoundKey = compoundKeyValue()
}


dynamic lazy var compoundKey: String = self.compoundKeyValue()

override static func primaryKey() -> String? {
return “compoundKey”
}

func compoundKeyValue() -> String {
    return "\(svID)\(solutionTitle)\(videoName)\(relatedInfo)\(shortDesc)”
}

}
Edison
  • 11,881
  • 5
  • 42
  • 50
  • I am using AlamofireObjectMapper, it combines alamofire and objectmapper. – Janice Zhan Jul 04 '16 at 02:01
  • See my update. Will that work? Using custom setters you're making sure that the compoundKey is always updated. lazy var ensures that the first time you access the property it will be gotten from what you had set. – Edison Jul 04 '16 at 02:45
  • It's not right. When I tried to map that, it throws an error, that svID is get-only – Janice Zhan Jul 04 '16 at 02:48
0

The main thing I can think of is that the primary key values are coming down as strings instead of proper integers (i.e., "10" instead of simply 10). It's possible that the mapper isn't smart enough to handle conversion of strings to integers, so it's just defaulting to 0.

According to the ObjectMapper documentation, you should be able to perform this conversion inline in your mapping function:

svID <- (map["svID"], TransformOf<Int, String>(fromJSON: { Int($0!) }, toJSON: { $0.map { String($0) } }))

Also, as a sidenote, I noticed that one of your JSON key names has a spelling error: "realtedInfo". So I'd recommend double-checking that works too. :)

TiM
  • 15,812
  • 4
  • 51
  • 79