该领域已经在/ Users / cm / Realm /中进行写事务

The*_*heo 3 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) 
Run Code Online (Sandbox Code Playgroud)

我的代码是

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


}
Run Code Online (Sandbox Code Playgroud)

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

@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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢,

西奥

更新

我正在后台线程中执行写事务。

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


   }
Run Code Online (Sandbox Code Playgroud)

但是现在我有这个例外。

  12-02 08:35:10.686 1129-1484/? E/Drm: Failed to find drm plugin
Run Code Online (Sandbox Code Playgroud)

Epi*_*rce 5

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

@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());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 或者,您可以使用realm.isInTransaction()在执行之前检查领域是否在Transaction中。 (2认同)

Md.*_*rim 5

在Realm中进行任何操作之后realm.close()都是一件重要的事情。在写入领域之前,如果您有任何与领域相关的查询(例如读取、删除),则必须关闭领域对象。如果您错过了这一点,那么在写入过程中您将收到此类错误。

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

这是示例代码:

Realm realm = Realm.getDefaultInstance();
            realm.beginTransaction();
            result.deleteFromRealm();
            realm.commitTransaction();
            realm.close();
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助 :)