当我将查询结果作为observable返回时,我必须在哪里关闭realm实例?例如:
public Observable<RealmResults<Weather>> getWeatherForecast() {
final Realm realm = Realm.getDefaultInstance();
return realm.where(Weather.class).findAllAsync().asObservable()
.filter(RealmResults::isLoaded);
}
答案 0 :(得分:2)
有几种情况:
doOnCompleted
运营商:
realm.where(Weather.class).findAllAsync().asObservable()
.filter(RealmResults::isLoaded)
.first()
.doOnCompleted(new Action0() {
@Override
public void call() {
realm.close();
};
})
在Subscriber
onCompleted
方法内:
getWeatherForecast()
.subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
realm.close();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Boolean aBoolean) {
}
});