title: Copying all objects (preserving their relationships) from one Realm to another
body: <p><strong>tl;dr: I'm trying to copy every single object from one Realm to another, but I get twice as many objects or 4 times as many objects as I should (because of their relationships, I presume).</strong> (Edit: I actually get many many more! Check my last edit at the bottom.)</p>
  
  <p>I'm trying to allow my users to backup and restore their Realm databases.</p>
  
  <p>I have a <code>Book</code> class and a <code>ReadingSession</code> class. A <code>Book</code> can have many <code>ReadingSessions</code>:</p>
  
  <pre><code>class Book: Object {
      // (…)
      var readingSessions: [ReadingSession] {
          return linkingObjects(ReadingSession.self, forProperty: "book")
      }
  }
  
  class ReadingSession: Object {
      // (…)
      var book: Book?
  }
  </code></pre>
  
  <p>To restore from the backup I tried doing this:</p>
  
  <pre><code>func restoreBackupFile(backupFileToRestore: String) {
      // (…) I omitted the NSFileManager related part.
      let config = Realm.Configuration(path: "\(tmp)/ReadingLog.realm", readOnly: true)
      let backupRealm = try! Realm(configuration: config)
      let defaultRealm = try! Realm()
  
      let results = backupRealm.objects(Book)
  
      try! defaultRealm.write {
  
          for result in results {
              defaultRealm.create(Book.self, value: result)
          }
      }
  </code></pre>
  
  <p>That copied all the <code>Book</code> objects alright, but not the <code>ReadingSessions</code> related to those <code>Books</code>.
  So I added some code to copy every <code>ReadingSession</code> too:</p>
  
  <pre><code>    // (…)
      let bookResults = backupRealm.objects(Book)
      let sessionResults = backupRealm.objects(ReadingSession)
  
      try! defaultRealm.write {
  
          for result in bookResults {
              defaultRealm.create(Book.self, value: result)
          }
          for result in sessionResults {
              defaultRealm.create(ReadingSession.self, value: result)
          }
      }
  </code></pre>
  
  <p>And that gave my <code>defaultRealm</code> 4 times as much books as it should! I used a database with 10 books to test it out, and after running that code my default Realm had 40 books, 20 with the right <code>ReadingSessions</code> associated to them and 20 without any <code>ReadingSessions</code> at all.</p>
  
  <p>I tried copying just the <code>ReadingSessions</code> to see if the related <code>Books</code> would be created too, and then I got twice as many <code>Books</code> as I should, half of them with the right <code>ReadingSessions</code> and half of them without any.</p>
  
  <p>So how can I do what I want? Copy every single object from a Realm to another keeping their relationships intact and without getting duplicates like I am now?</p>
  
  <p>(I know I can just replace the database files, but I want my users to be able to restore data from one database without losing the data from the other.)</p>
  
  <p>Thanks in advance,</p>
  
  <p>Daniel</p>
  
  <hr>
  
  <p><strong>Edit:</strong> I've been experimenting some more and it seems that if I copy just the <code>ReadingSessions</code> it creates a book for each reading session copied, even if they're related to the same book. If I have a book with 60 reading sessions, for instance, that book will be created 60 times. So my problem is even worse than I thought. When I copy the <code>Books</code> they don't come with their related <code>ReadingSessions</code>. When I copy the <code>ReadingSessions</code> they create many repeated books.</p>