我尝试使用Realm和Rx
Java实现模式Repository.存储库接口具有这样的签名
Observable<List<T>> query(Specification specification);
因此,当我使用Realm并想要检索结果“asObservable”时,我得到了Observable>.我找不到改变Observable的方法>进入Observable>.
任何人都可以帮忙解决这个问题吗?
我试过这样的事
final Observable<RealmResults<PlantRealm>> realmResults = realm.where(PlantRealm.class) .equalTo(PlantTable.ID,"1") .findAll().asObservable(); // convert Observable<RealmResults<PlantRealm>> into Observable<List<PlantRealm>> return realmResults.flatMap(Observable::from).toList().map(list -> { for (PlantRealm plantRealm : list) { plants.add(toPlant.map(plantRealm)); } return Observable.from(plants); } );
但类型检查系统仍在抱怨….
return Observable.create( subscriber -> { final Realm realm = Realm.getInstance(realmConfiguration); final RealmResults<PlantRealm> realmResults = realm.where(PlantRealm.class) .equalTo(PlantTable.ID,"1") .findAll(); final List<Plant> plants = new ArrayList<>(); for (PlantRealm plantRealm : realmResults) { plants.add(toPlant.map(plantRealm)); } realm.close(); subscriber.onNext(plants); subscriber.onCompleted(); });