将 Json 数据数组添加到 Realm

Adding an array of Json Data to Realm

我正在为机场制作一个应用程序,我正在从一个 api 中获取一组数据,如下所示:

"data":[
{"id":"001","code":"ABZ","name":"Aberdeen","country":"United Kingdom"},
{"id":"002","code":"AUH","name":"Abu Dhabi","country":"United Arab Emirates"},
.
.
.
]

和:

"airports":[
{"from":"001",
    "to":["1","3","11","13","12","20","23","27","29","31","33"]
},
.
.
.
]

我已经创建了领域模型类:

class AirportsDataRealm: Object {
    @objc dynamic var name: String = ""
    @objc dynamic var id: Int = 0
    @objc dynamic var code: String = ""
    @objc dynamic var country: String = ""
    override static func primaryKey() -> String? {
        return "id"
    }
}

class AirportsFromToRealm: Object {
    @objc dynamic var fromID: Int = 0
    var toID = List<Int>()
    override static func primaryKey() -> String? {
        return "fromID"
    }
}

现在我想将它保存到领域中,我正在使用 swiftyJSON 并使用 for-loop 来完成它并且它工作正常但我认为它需要很长时间因为数组很长,这里是我做了什么:

    // Airports Data
    let countData = json["data"].count
    for i in 0...countData - 1{
        let airportsDataModel = AirportsDataRealm()
        airportsDataModel.code = json["data"][i]["code"].stringValue
        airportsDataModel.name = json["data"][i]["name"].stringValue
        airportsDataModel.country = json["data"][i]["country"].stringValue
        airportsDataModel.id = Int(json["data"][i]["id"].stringValue)!
        try! realm.write {
            realm.add(airportsDataModel, update: true)
        }
    }

    //Airports FROM-TO
    let countFromTo = json["airports"].count

    for i in 0...countFromTo - 1{
        let fromToDataModel = AirportsFromToRealm()
        fromToDataModel.fromID = Int(json["airports"][i]["from"].stringValue)!
        let arrayTo = json["airports"][i]["to"].arrayValue.map{ [=14=].intValue }
        fromToDataModel.toID.append(objectsIn: arrayTo)

        try! realm.write {
            realm.add(fromToDataModel, update: true)
        }
    }

有什么方法可以在不使用 for 循环的情况下一次性将整个数组保存在 realm 中?

P.S "there should be a relation between the two tables because each from 'id' has a list of 'to' id's and the id's are from the data table, for now I managed to create this relations when fetching the data using filters ,, so just ignore this"

谢谢

直接用map方法,

首先,我需要向我的对象 类 添加初始化程序并将 json 数组作为参数传递,如下所示:

class AirportsDataRealm: Object {
    @objc dynamic var name: String = ""
    @objc dynamic var id: Int = 0
    @objc dynamic var code: String = ""
    @objc dynamic var country: String = ""
    convenience required init(withJSON json : JSON) {
        self.init()
        self.name = json["name"].stringValue
        self.id = json["id"].intValue
        self.code = json["code"].stringValue
        self.country = json["country"].stringValue
    }
    override static func primaryKey() -> String? {
        return "id"
    }
}


class AirportsFromToRealm: Object {
    @objc dynamic var fromID: Int = 0
    var toID = List<Int>()
    convenience required init(withJSON json : JSON) {
        self.init()
        self.fromID = json["from"].intValue
        let toArray = json["to"].arrayValue.map{ [=10=].intValue }
        self.toID.append(objectsIn: toArray)
    }

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

然后通过使用 map 方法,代码将如下所示:

func updateAirport(json: JSON) {
        // Airports Data
        let airportsData : [AirportsDataRealm]
        let airportsDataJsonArray = json["data"].array
        airportsData = airportsDataJsonArray!.map{AirportsDataRealm(withJSON: [=11=])}
        //Airports FROM-TO
        let airportsFromTo : [AirportsFromToRealm]
        let airportsFromToJsonArray = json["airports"].array
        airportsFromTo = airportsFromToJsonArray!.map{AirportsFromToRealm(withJSON: [=11=])}
        //Write To Realm
        try! realm.write {
            realm.add(airportsData, update: true)
            realm.add(airportsFromTo, update: true)
        }
}

没有for循环了^_^