title: Realm : multiple write at the same time
body: <p><strong>Context</strong></p>
  
  <p>I have problems using the method <code>realm.add(object, update: true)</code> successively.</p>
  
  <p>I'm making WEB requests to my API, and I'm testing the case where internet connection is disabled. When a request failed and I get the response, I add an <code>UnsynchronizedRequest</code> realm object to <code>Realm</code>.</p>
  
  <p><strong>Problem</strong> </p>
  
  <p>I have to test the case where I have to make multiples call to my API, so I will add multiple <code>UnsynchronizedRequest</code> objects to <code>Realm</code>.</p>
  
  <p>I only start the next web request when I received the previous request response, so the requests are well chaining, there is no concurrent requests.</p>
  
  <p>When I'm making <strong>only one</strong> request and that it failed, the object is well added to <code>Realm</code>.</p>
  
  <p>But when I'm making <strong>more than one request</strong>, <strong>only the first is added</strong> to the Realm, and the others are not added.</p>
  
  <p>What is strange is that when I'm <strong>using breakpoints</strong>, <strong>all objects are well added to Realm</strong>. But when I disable breakpoints, <strong>only the first <code>UnsynchronizedRequest</code> object</strong> is added to the Realm, the other are ignored.</p>
  
  <p>I think it is a problem with the <code>write</code> thread, the doc says that it blocks other thread if multiple writes are concurrent, but I don't know how to solve it.</p>
  
  <p><strong>What I execute when a web request failed :</strong> </p>
  
  <pre><code>class func createUnsynchronizedRequest(withParameters parameters : Parameters, andRoute route : String){
      print("Adding unsynchronized request to local...")
      let failedRequest = UnsynchronizedRequest()
      failedRequest.parameters = parameters
      failedRequest.route = route
      failedRequest.date = Date()
      RealmManager.sharedInstance.addOrUpdate(object: failedRequest)
  }
  </code></pre>
  
  <p><strong>RealmManager.swift</strong></p>
  
  <pre><code>func addOrUpdate(object : Object){
      try! realm.write {
          realm.add(object, update: true)
      }
  }
  </code></pre>
  
  <p><strong>UnsynchronizedRequest.swift</strong> </p>
  
  <pre><code>@objcMembers class UnsynchronizedRequest : Object {
  
  
  // MARK: - Realm Properties
  
  override static func primaryKey() -&gt; String? {
      return "id"
  }
  
  dynamic var id = ""
  dynamic var route = ""
  dynamic var date : Date! {
      didSet {
          self.id = "\(self.date)"
      }
  }
  }
  </code></pre>
  
  <p>I already tried to check if realm was in transaction in the <code>addOrUpdate</code> method before starting <code>realm.write</code>, this not solve the problem.</p>
  
  <p>I also tried to catch the error with <code>realm.write</code>, but no error is thrown.</p>
  
  <p>Furthermore, if I execute for example 3 time a web request et that is failed, I'm sure that my code is executed because the <code>print</code> in <code>createUnsynchronizedRequest</code> is working.</p>
  
  <p>I really think it is a problem with <code>write threads</code> because when I use breakpoint to slow the code execution, everything works well.</p>
  
  <p><strong>Example with 3 failing web requests and USING BREAKPOINTS</strong> :</p>
  
  <p><a href="https://i.stack.imgur.com/4jO28.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4jO28.gif" alt="enter image description here"></a></p>
  
  <p>As you can see, using breakpoints, the 3 objects are well added to realm : </p>
  
  <p><a href="https://i.stack.imgur.com/5OSGu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5OSGu.png" alt="enter image description here"></a></p>
  
  <p><strong>Example with the same 3 failing web requests and WITHOUT BREAKPOINTS</strong> :</p>
  
  <p><a href="https://i.stack.imgur.com/0H2T8.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0H2T8.gif" alt="enter image description here"></a></p>
  
  <p>But now without breakpoints, only the first object is added to Realm : </p>
  
  <p><a href="https://i.stack.imgur.com/JMWeX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JMWeX.png" alt="enter image description here"></a></p>
  
  <p>Any idea ? </p>