Realm - java.lang.IllegalStateException:对象不再有效进行操作

时间:2018-05-29 14:57:34

标签: android realm

我不断在Fabric上遇到这种影响,这会影响我们的一些用户,但它很难复制。它似乎是随机发生的,大部分时间都可以正常工作。 û

Fatal Exception: java.lang.IllegalStateException: Object is no longer valid to operate on. Was it deleted by another thread?
   at io.realm.internal.UncheckedRow.nativeGetString(UncheckedRow.java)
   at io.realm.internal.UncheckedRow.getString(UncheckedRow.java:157)
   at io.realm.com_pitchero_ph_features_schedule_model_training_SessionRealmProxy.realmGet$venue(com_pitchero_ph_features_schedule_model_training_SessionRealmProxy.java:275)
   at com.pitchero.ph.databinding.RowScheduleTrainingBinding.executeBindings(RowScheduleTrainingBinding.java:125)
   at android.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:437)
   at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:409)
   at android.databinding.ViewDataBinding$7.run(ViewDataBinding.java:194)
   at android.databinding.ViewDataBinding$8.doFrame(ViewDataBinding.java:286)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:957)
   at android.view.Choreographer.doCallbacks(Choreographer.java:734)
   at android.view.Choreographer.doFrame(Choreographer.java:667)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6776)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

将对象保存到Realm,如下所示:

 public static void saveRealmObjects(final RealmList<RealmObject> objects,
                                    Realm.Transaction.OnSuccess onSuccess,
                                    Realm.Transaction.OnError error) {
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                bgRealm.copyToRealmOrUpdate(objects);
            }
        }, onSuccess, error);
    } catch (Exception e) {
        Log.e(LOG_TAG, "saveRealmObjects error: " + e.getMessage());
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}

并且有一个查询,其中附加了一个侦听器以接收更新并相应地更新UI:

 dbQueryFixtures = query
            .equalTo(Fixture.FIELD_CLUB_ID, String.valueOf(club.clubId))
            .equalTo(Fixture.FIELD_MEMBER_ID, getMemberId())
            .findAllAsync();
    dbQueryFixtures.addChangeListener(new RealmChangeListener<RealmResults<Fixture>>() {
        @Override
        public void onChange(RealmResults<Fixture> fixtures) {
            fixtures.sort(Fixture.FIELD_DATE, Sort.ASCENDING);
            final RealmList<Fixture> results = new RealmList<>();
            results.addAll(dbQueryFixtures);
            getEventsHistoryFromDB(results, logic);
        }
    });

收到更新后,结果将传递给RecyclerView,后者会更新视图。每个视图都使用DataBinding从对象中获取值。例如:

<data>

    <variable
        name="session"
        type="Session"/>

    <import type="android.text.TextUtils"/>

    <import type="android.view.View"/>
</data>

<TextView
                android:id="@+id/schedule_training_subtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:includeFontPadding="false"
                android:layout_marginTop="5dp"
                android:text="@{session.venue}"
                android:textColor="@color/grayLight"
                android:textSize="13sp"
                tools:text="Training session"/>

如果有人能指出我正确的方向,我将非常感激,因为我无法找到它的底部。

0 个答案:

没有答案