中寫入事務當我運行我的應用程序時,發生此異常。我該如何解決它?Realm已經在/ Users/cm/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());
}
}
所有我想要的,現在是顯示標題,後來就嘗試新事物,像許多一對多的關係等
感謝,
西奧。
UPDATE
我在後臺線程做寫入事務。
@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
不支持嵌套事務,因此在連續3次打開事務通常也不起作用。 – EpicPandaForce
你應該考慮關閉後臺線程上的Realm實例,當你完成它 – EpicPandaForce