Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Realm is already in a write transaction in /Users/cm/Realm/

Tags:

android

realm

I have this exception when I am running my app. How can I fix it?

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) 

And my code is

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);
  }


}

And I am putting so dummy data inside my main fragment.

@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<Task> tasks = realm.where(Task.class).findAll();

    for(Task t:tasks){
        Log.d("Realm",t.getTitle());
    }
}

All I want for now is to display the title,and later on to try new things like many-to-many relationships etc.

Thanks,

Theo.

UPDATE

I am doing the write transaction in a background thread.

  @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<Task> tasks = realm.where(Task.class).findAll();

            for(Task task : tasks) {
                Log.d("Realm", task.getTitle());
            }

        }
      };

          handler.postDelayed(r, 1000);


   }

But now I have this exception.

  12-02 08:35:10.686 1129-1484/? E/Drm: Failed to find drm plugin
like image 634
Theo Avatar asked Dec 02 '16 09:12

Theo


2 Answers

It doesn't work because you are opening a new transaction inside a transaction. Twice.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    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<Task> tasks = realm.where(Task.class).findAll();

    for(Task task : tasks) {
        Log.d("Realm", task.getTitle());
    }
}
like image 116
EpicPandaForce Avatar answered Oct 17 '22 17:10

EpicPandaForce


After any operation in Realm realm.close() is an important things. Before writing on realm if you have any realm related query like read, delete you have to close the realm object. If you miss this then during write you will get this types of error.

Please check your repository class where you missed this realm.close() method call.

Here is the sample code:

Realm realm = Realm.getDefaultInstance();
            realm.beginTransaction();
            result.deleteFromRealm();
            realm.commitTransaction();
            realm.close();

Hope this will help you :)

like image 3
Md. Sajedul Karim Avatar answered Oct 17 '22 17:10

Md. Sajedul Karim