2017-03-04 3 views
6

Ich versuche json Objekt realm Objekt zu speichern unter Verwendung Objectmapper nachdem ich Antwort von Alamofire erhalten. Unten ist der Code, den ich geschrieben habe:Verbindung Schlüsselfrage Realm Swift

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") 
     } 
    } 
} 

Ich bin in der Lage json Daten von meinem Server abzubilden. Jedoch gibt es ein Problem mit meinem zusammengesetzten Schlüssel. Die drei Variablen sind nicht eindeutig, aber ihre Kombination ist eindeutig. Daher musste ich compoundKey als Primärschlüssel verwenden. Ich baue primaryKey von compoundKey wie folgt:

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)" 
} 

Dies ist, wo ich meine drei Variablen initialisiert.

func setCompoundID(yearNp: Int, mahina: String, gate: Int) { 
    self.yearNp = yearNp 
    self.mahina = mahina 
    self.gate = gate 
    compoundKey = compoundKeyValue() 
} 

und die Definition von compoundKey gemäß Github issues ist hier. Ich habe 31 Wörterbücher in meiner Datenbank gespeichert, aber ich kann nur das letzte Wörterbuch speichern. Ich bin mir sicher, dass dies ein zusammengesetztes Schlüsselthema ist, da diese Codebasis Daten in einer anderen Tabelle speichern kann, die ein eindeutiges Feld als Primärschlüssel hat, was in dieser Datenbanktabelle nicht der Fall ist. Habe ich meine compoundKey falsch deklariert?

Antwort

1

Ich habe Alamofire nicht verwendet, so nahm ich an, dass Ihr Code auf Alamofire Teil korrekt war. Sie haben die Struktur Ihres JSON nicht angegeben, ich nahm an, dass Ihr JSON 31 Wörterbücher enthielt. Außerdem nahm ich an, dass die Realm-Datenbank zu Beginn leer war. Wenn nicht, mach es bitte leer.

Ich glaube, das Problem ist hier.

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

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

Bitte ändern Sie es in

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 
} 

und Ihr Projekt. Wenn Realm einen doppelten ID-Fehler auslöst, müssen Ihre compoundKey s nach der Initialisierung nicht erfolgreich geändert werden. Dann sollten Sie diesen Teil überprüfen. Vielleicht sollten Sie es manuell aufrufen oder den entsprechenden Teil Ihrer init Funktion überschreiben.

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) 
}