王国关闭自己

时间:2017-04-25 19:44:54

标签: android realm rx-java

我在Application对象上使用一个静态全局Realm实例(从不关闭)仅用于UI Thread,

@UiThread
public static Realm getRealm() {
    if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
        return realmInstance;
    } else {
        Timber.e("Illegal access to getRealmObservable");
        throw new IllegalStateException("Only UI Thread can access this realm");
    }
}

另一个单独使用WorkerThread的域如下:

@WorkerThread
public static void executeOnSingleUseRealm(final Realm.Transaction transaction) {
    if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
        Timber.e("Wrong thread for Realm");
        throw new IllegalStateException("Wrong thread for Single use Realm");
    }

    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        realm.executeTransaction(transaction);
    } catch (Exception e) {
        Timber.e(e, "Exception in Single Use Realm transaction");
        throw e;
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}

但是,我仍然看到使用单个全局Realm实例时出现崩溃:This Realm instance has already been closed, making it unusable.

我不知道怎么可能。

以下是我初始化Realm实例的方法:

申请onCreate

void onCreate(){
    ....
        Observable
            .fromCallable(() -> {
                Realm.init(SVApplication.this);
                RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                        .deleteRealmIfMigrationNeeded()
                        .build();
                Realm.compactRealm(realmConfiguration);
                Realm.setDefaultConfiguration(realmConfiguration);
                return true;
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((v) -> onRealmLoaded());
    }
}

void onRealmLoaded(){
    realmInstance = Realm.getDefaultInstance();
    ....
}

以下是其中一项活动的崩溃之一:

void onStart(){
    ....    subscribeUntilDetach(realmInstance.where(Notification.class).findAllAsync().asObservable()
                        .onBackpressureLatest()
                        .switchIfEmpty(emptyNotification())
                        .map(notifications -> notifications.where().isNull("readTime").or().isEmpty("readTime").count())
                        .onBackpressureLatest()
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(count -> {
                            if (count == 0L) {
                                mNotificationBadge.setVisibility(View.GONE);
                            } else {
                                mNotificationBadge.setText(String.format(Locale.getDefault(), "%d", count));
                                mNotificationBadge.setVisibility(View.VISIBLE);
                            }
                        }, throwable -> Timber.e(throwable, "Error setting notification count")));
}

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        mCompositeSubscription.clear();
    }
}

protected void subscribeUntilDetach(@NonNull Subscription subscription) {
    mCompositeSubscription.add(subscription);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mCompositeSubscription.hasSubscriptions()) {
        mCompositeSubscription.unsubscribe();
    }
}

在某些活动中,此行也会因同样的错误而崩溃:

mCompositeSubscription.unsubscribe();

这是来自Crashlytics的堆栈跟踪,可能不完全准确。

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.myapp.mobile/com.teknoloji.myapp.ui.pages.HomeActivity}: rx.b.f: This Realm instance has already been closed, making it unusable.
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3353)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3384)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1443)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:168)
   at android.app.ActivityThread.main(ActivityThread.java:5885)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by rx.b.f: This Realm instance has already been closed, making it unusable.
   at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(Unknown Source)
   at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(Unknown Source)
   at rx.internal.util.ActionSubscriber.onError(Unknown Source)
   at rx.observers.SafeSubscriber._onError(Unknown Source)
   at rx.observers.SafeSubscriber.onError(Unknown Source)
   at rx.exceptions.Exceptions.propagate(Unknown Source)
   at rx.observers.SafeSubscriber.onNext(Unknown Source)
   at rx.internal.producers.SingleProducer.request(Unknown Source)
   at rx.Subscriber.setProducer(Unknown Source)
   at rx.Subscriber.setProducer(Unknown Source)
   at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(Unknown Source)
   at rx.internal.operators.OperatorTake$1.onNext(Unknown Source)
   at rx.internal.operators.NotificationLite.next(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.accept(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitFirst(Unknown Source)
   at rx.subjects.BehaviorSubject$1.call(Unknown Source)
   at rx.subjects.BehaviorSubject$1.call(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager.call(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager.call(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.Observable.create(Unknown Source)
   at rx.Observable.unsafeCreate(Unknown Source)
   at rx.Observable.create(Unknown Source)
   at com.teknoloji.myapp.ui.pages.HomeActivity.onStart(Unknown Source)
   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1288)
   at android.app.Activity.performStart(Activity.java:6279)
   at android.app.Activity.performRestart(Activity.java:6325)
   at android.app.Activity.performResume(Activity.java:6330)
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3336)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3384)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1443)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:168)
   at android.app.ActivityThread.main(ActivityThread.java:5885)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
   at io.realm.BaseRealm.checkIfValid(Unknown Source)
   at io.realm.Realm.init(Unknown Source)
   at com.teknoloji.myapp.ui.pages.HomeActivity.lambda$onStart$3(Unknown Source)
   at com.teknoloji.myapp.ui.pages.HomeActivity$$Lambda$1.call(Unknown Source)
   at rx.internal.util.ActionSubscriber.onNext(Unknown Source)
   at rx.observers.SafeSubscriber.onNext(Unknown Source)
   at rx.internal.producers.SingleProducer.request(Unknown Source)
   at rx.Subscriber.setProducer(Unknown Source)
   at rx.Subscriber.setProducer(Unknown Source)
   at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(Unknown Source)
   at rx.internal.operators.OperatorTake$1.onNext(Unknown Source)
   at rx.internal.operators.NotificationLite.next(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.accept(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitFirst(Unknown Source)
   at rx.subjects.BehaviorSubject$1.call(Unknown Source)
   at rx.subjects.BehaviorSubject$1.call(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager.call(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager.call(Unknown Source)
   at rx.subjects.SubjectSubscriptionManager.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.internal.operators.OnSubscribeLift.call(Unknown Source)
   at rx.Observable.create(Unknown Source)
   at rx.Observable.unsafeCreate(Unknown Source)
   at rx.Observable.create(Unknown Source)
   at com.teknoloji.myapp.ui.pages.HomeActivity.onStart(Unknown Source)
   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1288)
   at android.app.Activity.performStart(Activity.java:6279)
   at android.app.Activity.performRestart(Activity.java:6325)
   at android.app.Activity.performResume(Activity.java:6330)
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3336)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3384)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1443)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:168)
   at android.app.ActivityThread.main(ActivityThread.java:5885)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

2 个答案:

答案 0 :(得分:2)

问题是:

  

已关闭的异常表示您尝试使用已关闭的异常   领域,以一种不被推广为最好的方式利用Realm   实践。 ...每个活动都应该有自己的领域实例   为了最好的表现。 ...在onCreate()和。创建一个领域实例   在onDestory()上销毁实例。   https://github.com/realm/realm-java/issues/2594#issuecomment-211793848

此外

  

......在类中保留Realm的静态字段并不是一个好主意。建议在Activity / Fragment / etc https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances

中控制Realm的生命周期

因为在Realm中创建新实例,当至少有一个打开时,超快,不要害怕创建Realm的多个实例。

Realm的高级软件工程师指出here

  

只要在调用Realm.getInstance()的线程上至少打开一个实例,它就只是一个HashMap查找...最好的做法是在你的线程存在的时候保持Realm实例打开。

最佳做法在Realm documentation

中有所描述
   <Components.MapView
      ref={(map) => this.map = map}
      style={{width: Metrics.screenWidth, height: mapHeight, zIndex: 0}}
      region={{
        latitude: this.state.location.coords.latitude,
        longitude: this.state.location.coords.longitude,
        latitudeDelta: 0.0100,
        longitudeDelta: 0.0100,
      }}
    >
      <Components.MapView.Marker
        key={'i29'}
        draggable
        onDragEnd={this.onUserPinDragEnd.bind(this)}
        title={'You are here'}
        coordinate={{
          latitude: this.state.userLocation.coords.latitude,
          longitude: this.state.userLocation.coords.longitude,
        }}
      />
   </Components.MapView>

对于工作线程,documentation recommends在开始时获取Realm的新实例,并在结束时关闭它。

// Setup Realm in your Application
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
        Realm.setDefaultConfiguration(realmConfiguration);
    }
}

// onCreate()/onDestroy() overlap when switching between activities.
// Activity2.onCreate() will be called before Activity1.onDestroy()
// so the call to getDefaultInstance in Activity2 will be fast.
public class MyActivity extends Activity {
    private Realm realm;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        realm = Realm.getDefaultInstance();
        // ...
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }
}

答案 1 :(得分:0)

我认为问题在RealmObservableFactory左右。我已删除所有Rx + Realm关系(.asObservable()等),使用旧学校实施并且进展顺利。