alamofire를 사용하여 서버에서 응답 JSON을 얻은 다음 ObjectMapper를 사용하여 문자열을 Realm 객체에 매핑합니다.JSON 문자열이있는 Realm + Swift 업데이트 객체
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)
}
}
}
}
문제는 내가 성공적으로 개체를 추가 할 수 있다는 것입니다. 하지만 svID (primark 키)는 10이나 9 대신 JSON으로 0을 유지합니다. 내가 svID에 기본값을 설정했기 때문인가? 누군가 나에게 힌트를 줄 수 있습니까? 감사합니다
이미 AlamofireObjectMapper 확장 프로그램을 사용하고 있습니까? – LNI
예, 이미 사용했습니다 :) –