title: Realm is already in a write transaction if I execute two transactions in the same method
body: <p>I need to eliminate the first records recorded by the realm db if they are more than a certain number, but I get this exception:</p>
  <pre><code>Caused by: java.lang.IllegalStateException: The Realm is already in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 126
      at io.realm.internal.OsSharedRealm.nativeBeginTransaction(Native Method)
      at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:269)
      at io.realm.BaseRealm.beginTransaction(BaseRealm.java:404)
      at io.realm.Realm.beginTransaction(Realm.java:145)
      at io.realm.Realm.executeTransaction(Realm.java:1491)
      atsavedAddresses(AddressBook.kt:39)````
  </code></pre>
  <p>As you can see below I tried to add <code>commitTransaction</code>, to play with <code>executeTransactionAsync</code>, and <code>executeTransaction</code>, begin transaction etc. But nothing is going to work although I read several so posts and GitHub issues comments , I even found this so <a href="https://stackoverflow.com/questions/39366182/the-realm-is-already-in-a-write-transaction">post</a> but is too abstract to relate to my case</p>
  <pre><code>init {
  
      Realm.init(ProActiveApplication.applicationContext())
  }
  
  
  private val realm = Realm.getDefaultInstance()
  
  val query = realm.where(RecentAddress::class.java).findAll()
  
  fun savedAddresses(name: String, address: String) {
     realm.beginTransaction()
      realm.executeTransactionAsync  {
          val addressToSave = realm.createObject(RecentAddress::class.java)
          addressToSave.name = name
          addressToSave.street = address
          Log.d(&quot;ME&quot;,&quot;-----------------&quot;)
          Log.d(&quot;ME&quot;, &quot; CreateTransaction: ${addressToSave.name} in ${addressToSave.street}&quot;)
          Log.d(&quot;ME&quot;,&quot;-----------------&quot;)
      }
      realm.commitTransaction()
      realm.close()
  
      realm.beginTransaction()
      realm.executeTransaction {
          if (query.size &gt; 2) {
              val location = query.get(0)
              Log.d(&quot;ME&quot;,&quot;-----------------&quot;)
  
              Log.d(&quot;ME&quot;, &quot; This is cancelled: ${query.get(0)}&quot;)
              Log.d(&quot;ME&quot;,&quot;-----------------&quot;)
  
              location?.deleteFromRealm()
  
          } }
  </code></pre>
  <p><em><strong>EDIT</strong></em></p>
  <p>As suggested I am going to eliminate <code>beginTransaction()</code> and <code>commitTransaction</code> also I changed to <code>executeTransaction</code> instead of `executeTransactionAsync()</p>
  <pre><code>    fun savedAddresses(name: String, address: String) {
     // realm.beginTransaction()
      realm.executeTransaction {
          val addressToSave = realm.createObject(RecentAddress::class.java)
          addressToSave.name = name
          addressToSave.street = address
          Log.d(&quot;ME&quot;, &quot;-----------------&quot;)
          Log.d(&quot;ME&quot;, &quot; CreateTransaction: ${addressToSave.name} in ${addressToSave.street}&quot;)
          Log.d(&quot;ME&quot;, &quot;-----------------&quot;)
      }
     // realm.commitTransaction()
      realm.close()
  
      // realm.beginTransaction()
      realm.executeTransaction {
          if (query.size &gt; 2) {
              val location = query.get(0)
              Log.d(&quot;ME&quot;, &quot;-----------------&quot;)
  
              Log.d(&quot;ME&quot;, &quot; This is cancelled: ${query.get(0)}&quot;)
              Log.d(&quot;ME&quot;, &quot;-----------------&quot;)
  
              location?.deleteFromRealm()
  
          }
  
      }
  </code></pre>
  <p>Now I get that is like Realm is going to cancel the first 0 position, but then is going to add the next record in the 0 position <strong>and not at end</strong> of the list</p>
  <p>If I cancel the second block <code>executeTransaction</code>  the record are successfully added in sequential order, but remaining the second block, the transaction eliminate row 0 but then insert the second record at the position 0, not at the end of the list</p>