Getting exception: "Already in a write transaction" in Realm for Xamarin iOS
<p>I'm using Realm in my Xamarin app, and I'm getting this exception in production. I have never seen it running the app myself, and it occurs only a few times daily. The complete stack trace is:</p>

<pre><code>System.AggregateException', reason: 'System.AggregateException: 
A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. 
---&gt; Realms.RealmInvalidTransactionException: The Realm is already in a write transaction at Realms.NativeException.ThrowIfNecessary (System.Func`2 overrider) &lt;0xab3dfc + 0x00038&gt; in &lt;filename unknown&gt;:0 
at Realms.SharedRealmHandle.BeginTransaction () &lt;0xab21c8 + 0x00043&gt; in &lt;filename unknown&gt;:0 
at Realms.Transaction..ctor (Realms.SharedRealmHandle sharedRealmHandle) &lt;0xab9ad8 + 0x0002f&gt; in &lt;filename unknown&gt;:0 
at Realms.Realm.BeginWrite () &lt;0xab7624 + 0x0003f&gt; in &lt;filename unknown&gt;:0 
at Realms.Realm.Write (System.Action action) &lt;0xab7674 + 0x0002b&gt; in &lt;filename unknown&gt;:0 
at MyApp.iOS.CollectionViewSource+&lt;GetCell&gt;c__AnonStorey0.&lt;&gt;m__2 (System.Threading.Tasks.Task`1 value) &lt;0x327c34 + 0x001b3&gt; in &lt;filename unknown&gt;:0 
at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () &lt;0x532fa8 + 0x00077&gt; in &lt;filename unknown&gt;:0 
at System.Threading.Tasks.Task.Execute () &lt;0x52ed88 + 0x0005b&gt; in &lt;filename unknown&gt;:0 
</code></pre>

<p>I'm sure that my app only have one write transaction opened at once. The only thing that I think may be causing problems is that the realm transaction don't close properly. My code is something like this:</p>

<pre><code>// this is called at some point in the app, when entering in a specific view controller
var transaction = realm.BeginWrite (); 
</code></pre>

<p>The code for close this Controller is:</p>

<pre><code>public void Close(bool saveChanges) {
    if (saveChanges) {
        transaction.Commit ();
        transaction.Dispose ();
    }       
    else {
        transaction.Rollback ();
        transaction.Dispose ();
    }

    // Ignore the fact that this code can be included in the previous section
    // It is just for making clear that after closing the transaction I can make 
    // a simple change to the objects
    if (saveChanges) {
        realm.Write(() =&gt; { 
            model.id = 1;
        }
    }

    NavigationController.PopToRootViewController (true);
}
</code></pre>

<p>When the root view controller is shown, then it accesses to the realm data and in this moment is when the crash occurred.</p>

<p>Any hint regarding to what can be happening here?</p>