Realm已经在/ Users / cm / Realm /中的写入事务中

时间:2016-12-02 09:28:49

标签: android realm

运行我的应用时出现此异常。我该如何解决?

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) 

我的代码是

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


}

我在主片段中放入了如此虚拟的数据。

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

我现在想要的只是显示标题,然后再尝试像多对多关系等新事物。

谢谢,

西奥。

更新

我在后台线程中进行写入事务。

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


   }

但现在我有这个例外。

  12-02 08:35:10.686 1129-1484/? E/Drm: Failed to find drm plugin

2 个答案:

答案 0 :(得分:3)

它不起作用,因为您在事务中打开一个新事务。两次。

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

答案 1 :(得分:2)

在Realm realm.close()中执行任何操作都是重要的事情。在写入领域之前如果你有任何领域相关的查询,如read,delete,你必须关闭领域对象。如果你错过了这个,那么在写作期间你会得到这种类型的错误。

请检查您错过此realm.close()方法调用的存储库类。

以下是示例代码:

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

希望这会对你有所帮助:)。