title: The Realm is already in a write transaction in /Users/cm/Realm/
body: <p>I have this exception when I am running my app. How can I fix it?</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: theo.testing.realmapplication, PID: 3359
                java.lang.RuntimeException: Unable to start activity ComponentInfo{theo.testing.realmapplication/theo.testing.realmapplication.MainActivity}: 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_SharedRealm.cpp line 113
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:148)
                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                 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_SharedRealm.cpp line 113
                    at io.realm.internal.SharedRealm.nativeBeginTransaction(Native Method)
                    at io.realm.internal.SharedRealm.beginTransaction(SharedRealm.java:219)
                    at io.realm.BaseRealm.beginTransaction(BaseRealm.java:328)
                    at io.realm.Realm.beginTransaction(Realm.java:125)
                    at theo.testing.realmapplication.MainFragment$1.execute(MainFragment.java:47)
                    at io.realm.Realm.executeTransaction(Realm.java:1253)
                    at theo.testing.realmapplication.MainFragment.onActivityCreated(MainFragment.java:43)
                    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1133)
                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
                    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                    at android.app.Activity.performStart(Activity.java:6253)
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  </code></pre>
  
  <p>And my code is</p>
  
  <pre><code>public class RealmExampleApplication extends Application {
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      Realm.init(this);
  
  
      RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
              .name("examples.db")
              .build();
  
      Realm.setDefaultConfiguration(realmConfiguration);
    }
  
  
  }
  </code></pre>
  
  <p>And I am putting so dummy data inside my main fragment. </p>
  
  <pre><code>@Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
      realm = Realm.getDefaultInstance();
  
      realm.executeTransaction(new Realm.Transaction() {
          @Override
          public void execute(Realm realm) {
              //create task
              realm.beginTransaction();
  
              Task t = realm.createObject(Task.class);
              t.setId(UUID.randomUUID().toString());
              t.setTitle("Hello");
              t.setDescription("This is description");
  
              realm.beginTransaction();
          }
      });
  
     RealmResults&lt;Task&gt; tasks = realm.where(Task.class).findAll();
  
      for(Task t:tasks){
          Log.d("Realm",t.getTitle());
      }
  }
  </code></pre>
  
  <p>All I want for now is to display the title,and later on to try new things like many-to-many relationships etc.</p>
  
  <p>Thanks,</p>
  
  <p>Theo.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am doing the write transaction in a background thread.</p>
  
  <pre><code>  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      Handler handler = new Handler();
  
      final Runnable r = new Runnable() {
          public void run() {
              realm = Realm.getDefaultInstance();
  
              realm.executeTransaction(new Realm.Transaction() { // TODO: move write to background thread
                  @Override
                  public void execute(Realm realm) {
                      //create task
  
                      Task t = realm.createObject(Task.class, UUID.randomUUID().toString()); // FIXME: if this doesn't work, `id` is not annotated with `@PrimaryKey`
                      t.setTitle("Hello");
                      t.setDescription("This is description");
                  }
              });
  
              RealmResults&lt;Task&gt; tasks = realm.where(Task.class).findAll();
  
              for(Task task : tasks) {
                  Log.d("Realm", task.getTitle());
              }
  
          }
        };
  
            handler.postDelayed(r, 1000);
  
  
     }
  </code></pre>
  
  <p>But now I have this exception.</p>
  
  <pre><code>  12-02 08:35:10.686 1129-1484/? E/Drm: Failed to find drm plugin
  </code></pre>