title: Adding an array of Json Data to Realm
body: <p>I'm making an app for airports and I'm getting an array of data from one api, like so:</p>
  
  <pre><code>"data":[
  {"id":"001","code":"ABZ","name":"Aberdeen","country":"United Kingdom"},
  {"id":"002","code":"AUH","name":"Abu Dhabi","country":"United Arab Emirates"},
  .
  .
  .
  ]
  </code></pre>
  
  <p>AND :</p>
  
  <pre><code>"airports":[
  {"from":"001",
      "to":["1","3","11","13","12","20","23","27","29","31","33"]
  },
  .
  .
  .
  ]
  </code></pre>
  
  <p>I have created realm model classes:</p>
  
  <pre><code>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() -&gt; String? {
          return "id"
      }
  }
  
  class AirportsFromToRealm: Object {
      @objc dynamic var fromID: Int = 0
      var toID = List&lt;Int&gt;()
      override static func primaryKey() -&gt; String? {
          return "fromID"
      }
  }
  </code></pre>
  
  <p>now I want to save it into realm, I'm using swiftyJSON and I have used for-loop to do it and it is working fine but I think it's taking long time since the array is very long, here is what I've done:</p>
  
  <pre><code>    // 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)
          }
      }
  </code></pre>
  
  <p>is there any way to save the whole array in realm in one shot without for-loop?</p>
  
  <p>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"</p>
  
  <p>Thank you</p>