title: Realm.io API mapping parent object automatically
body: <p>I am using Realm and its <a href="https://realm.io/docs/swift/latest/#rest-apis" rel="nofollow">REST API's</a> mapping method to map JSON data to my models and their child relationships automatically. It works great, except when I add in a <a href="https://realm.io/docs/swift/latest/#to-one" rel="nofollow">"To-One"</a> relationship on the child models referencing the parent. The mapping goes all out of whack and creates additional entries that don't exist.</p>
  
  <p>The parent model:</p>
  
  <pre><code>class Foo: Object {
    dynamic var id = 0
    dynamic var title = ""
    let bars = List&lt;Bar&gt;() // Children
  
    override class func primaryKey() -&gt; String {
      return "id"
    }
  }
  </code></pre>
  
  <p>The child model:</p>
  
  <pre><code>class Bar: Object {
    dynamic var id = 0
    dynamic var title = ""
    dynamic var foo = Foo() // Reference to parent
  
    override class func primaryKey() -&gt; String {
        return "id        
    }
  }
  </code></pre>
  
  <p>Per the <a href="https://realm.io/docs/swift/latest/#example" rel="nofollow">documentation</a> I am mapping the JSON using the convenience method:</p>
  
  <pre><code>let foos = JSON["foos"] as! [NSDictionary]
    let realm = Realm()
    realm.write {
      for foo in foos {
        realm.create(Foo.self, value: foo, update: true)
      }
    }
  </code></pre>
  
  <p>I'm aware I could set up the mapping manually, but I want to make use of the convenience method if possible at all. Does anyone know how to set this up?</p>