请问有人能分享一个带有Looper的后台线程的实现,我可以将其传递给subscribeOn(AndroidScheduler.from(/backgroundThreadWithLooper/))。
我需要这个是因为我正在尝试实现一个DBService类,在后台运行所有操作,同时仍然能够获取实时对象更新。所以当我应用addChangeListener时,会抛出异常:
java.lang.IllegalStateException: Your Realm is opened from a thread without a Looper. Async queries need a Handler to send results of your query
如果我使用findAll()而不是findAllAsync():
java.lang.IllegalStateException: You can't register a listener from a non-Looper thread or IntentService thread.
DBService 代码:
public Observable<List> getAll(Class clazz) {
return Observable.defer(() -> {
Realm realm = Realm.getDefaultInstance();
return realm.where(clazz).findAll().asObservable()
.map(o -> realm.copyFromRealm((RealmResults) o))
.doOnUnsubscribe(() -> closeRealm(realm))
.doOnTerminate(() -> closeRealm(realm));
});
}
HandlerThread
。 - pskink