将 Json 数据数组添加到 Realm
Posted
技术标签:
【中文标题】将 Json 数据数组添加到 Realm【英文标题】:Adding an array of Json Data to Realm 【发布时间】:2018-11-18 06:30:14 【问题描述】:我正在为机场制作一个应用程序,我正在从一个 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 $0.intValue
fromToDataModel.toID.append(objectsIn: arrayTo)
try! realm.write
realm.add(fromToDataModel, update: true)
有没有什么方法可以在没有for循环的情况下一次性将整个数组保存在领域中?
附言 “这两个表之间应该存在关系,因为来自‘id’的每个表都有一个‘to’id的列表,而id来自数据表,现在我在使用过滤器获取数据时设法创建了这种关系,所以忽略这个”
谢谢
【问题讨论】:
据我所知,访问领域和写入或添加对象需要花费大部分时间。所以我建议不要在 for 循环中执行此操作,将所有对象存储在一个数组中,并在 for 循环之后将数组添加到领域。 谢谢@hardikparmar 是的,它也可以这样工作,但我想完全删除 for 循环 【参考方案1】:简单地使用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 $0.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!.mapAirportsDataRealm(withJSON: $0)
//Airports FROM-TO
let airportsFromTo : [AirportsFromToRealm]
let airportsFromToJsonArray = json["airports"].array
airportsFromTo = airportsFromToJsonArray!.mapAirportsFromToRealm(withJSON: $0)
//Write To Realm
try! realm.write
realm.add(airportsData, update: true)
realm.add(airportsFromTo, update: true)
不再有 for 循环 ^_^
【讨论】:
以上是关于将 Json 数据数组添加到 Realm的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript:如何将数据添加到 JSON 文件中的数组