title: Appending to list property of persisted realm object?
body: <p>have to preface with saying im a beginner programmer pardon any other glaring mistakes. Working with a simple database of two objects, A Show objects and it's corresponding episodes, who's details are fetched from a json request. </p>
  
  <p>In the view that displays the persisted shows, i wrote a function to fire on a button press where the json file is checked for new &amp; or missing episodes not present in the Shows episode List property. The function that i wrote below produces the unable to add objects with existing primary keys. (Don't recall the exact name off the top of my head at the moment)</p>
  
  <p>I've done a test where i delete some episodes from the persisted shows and fire the method that initially saves the shows to the database and for some reason that accomplishes my desired functionality of adding only the deleted episodes without any crash or adding duplicates. </p>
  
  <p>Not sure of any other moves at this point. Much thanks in advance.</p>
  
  <p><strong>Realm constant in AppDelegate</strong></p>
  
  <pre><code>let database = try! realm()
  </code></pre>
  
  <p><strong>Realm Object Models</strong></p>
  
  <pre><code>Class Show: Object {
      dynamic var title: String? = nil
      var episodes = List&lt;Episode&gt;()
  
      class func showWithData (JSONShowObject) -&gt; Show? {
          let showTobeSaved = Show()
          showTobeSaved.title = JSONShowObject.title
  
          for episode in JSONShowObject.episodeArray {
              let episodeToBeSaved = Episode.episodeWithData(episode)
              showTobeSaved.episodes.append(episodeToBeSaved!)
          }
        return showToBeSaved
      }
  
      override static func primaryKey() -&gt; String? {
          return "title"
      }
  }
  
  class Episode: Object {
      dynamic var title: String? = nil
  
      class func episodeWithData (JSONEpisodeObject) -&gt; Episode? {
          let episodeToBeSaved = Episode()
          episodeToBeSaved.title = JSONEpisodeObject.title
          return episodeToBeSaved
      }
  
      override static func primaryKey() -&gt; String? {
          return "title"
      }
  }
  </code></pre>
  
  <p><strong>Function that adds show to Realm Database</strong></p>
  
  <pre><code>func saveShowToDatabase(JSONShowObject) {
      try! database.write() {
          database.add(Show.showWithData(JSONShowObject), update: true)
      }
  }
  </code></pre>
  
  <p><strong>View Controller for Persisted Shows</strong></p>
  
  <pre><code>class ShowVC: UIViewController {
  
  var shows: Results&lt;Show&gt;!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          shows = database.objects(Show.self)
      }
  
      @IBAction func updateShowEpisodes(_ sender: UIButton) {
          updateEpisodesForStoredShows(showsInDatabase: shows)
      }
  
      func updateEpisodesForStoredShows (showsInDatabase: Results&lt;Show&gt;!) {
          for showToUpdate in showsInDatabase {
              for episodeToBeSaved in JSONShowObject.episodeArray {
                  try! database.write() {
                      showToUpdate.episodes.append(episodeToBeSaved)
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Code Added after question answer</strong></p>
  
  <pre><code>//Added To Show Model
  func updateShowEpisodes() -&gt; Show ? {
      for episode in JSONShowObject.episode) {
          if let episodeToBeSaved = database.object(ofType: Show.self, forPrimaryKey: episode.title) {
              self.episodes.append(episodeToBeSaved)
          } else {
              self.episodes.append(Episode.episodeWithData(episode))
          }
      }
      return self
  }
  
  
  //Changed Func in VC For Persisted Shows
  func updateEpisodesForStoredShows(showsInDatabase: Results&lt;Show&gt;!) {
      for showToUpdate in showsInDatabase {
          try! database.write {
              database.add(showToUpdate.updateEpisodeFeed()!, update: true)
          }
      }
  }
  </code></pre>