title: RealmSwift: How to create To-One Relationships properly?
body: <p>Let's assume I have:</p>
  
  <pre><code>class Dog: Object {
      dynamic var race = ""
      dynamic var name = ""
  
      override static func primaryKey() -&gt; String? {
          return "race"
      }
  }
  
  class Person: Object {
      dynamic var name = ""
      dynamic var address = ""
      dynamic var dog: Dog?
  
      override static func primaryKey() -&gt; String? {
          return "name"
      }
  }
  </code></pre>
  
  <p>First I create a <code>Dog</code> and save it:</p>
  
  <pre><code>let dog = Dog()
  dog.race = "Dalmatian"
  
  try! realm.write {
      realm.add(dog, update: true)
  }
  </code></pre>
  
  <p>Now I create a <code>Person</code> in a different class. The docs are quite a bit unclear about this scenario. Do I need to save changes for the <code>Dog</code> first before creating the relationship?:</p>
  
  <pre><code>let person = Person()
  person.name = "Jim"
  
  // retrieve dog from realm: 
  
  if let dog = realm.objectForPrimaryKey(Dog.self, key: "Dalmatian") {
      dog.name = "Rex" // Owner gives dog a new name
  
      // Question:
      // Saving changes to Rex: is this step neccessary?
      try! realm.write {
           realm.add(dog, update: true)
      }
  
      person.dog = dog
  }
  
  try! realm.write {
      realm.add(person, update: true)
  }
  </code></pre>