i have following code, based off documentation provided realm (https://realm.io/docs/java/latest/#rxjava)
public observable<foo> getfoobyid(string id) { realm = realm.getinstance(realmconfiguration); return realm.where(foo.class) .equalto("id", id) .findfirstasync() .asobservable() .filter(this::filterresult); }
this works in app expected when comes testing things become little more tricky. have following test (stripped down keep simple) :
@test public void testrealmexample() { realmconfiguration config = new realmconfiguration.builder(context) .name("test.realm") .inmemory() .build(); datamanager datamanager = new datamanager(config); testsubscriber<foo> testsubscriber = new testsubscriber<>(); datamanager.getfoo("").observeon(androidschedulers.mainthread()).subscribe(testsubscriber); testsubscriber.assertnoerrors(); }
when test executed following error occurs java.lang.illegalstateexception: realm opened thread without looper. async queries need handler send results of query
.
to counter read on realm github page use annotation @uithreadtest
force test run on ui thread, understanding looper thread, therefore should solve problem. added:
@rule public final uithreadtestrule uithreadtestrule = new uithreadtestrule();
and changed test include annotation
@test @uithreadtest public void testrealmexample() { ...}
this still yields same exception. know why , solution? thanks.
the @uithreadtest
doesn't put on working looper thread, on thread access ui elements. must confess haven't looked details on why there difference. using custom rule looper threads instead (which cleans our realm instances). can see here , maybe use inspiration:
Comments
Post a Comment