use of io.realm.DynamicRealm in project realm-java by realm.
the class RealmObservableFactory method from.
@Override
public Flowable<DynamicRealm> from(DynamicRealm realm) {
if (realm.isFrozen()) {
return Flowable.just(realm);
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Flowable.create(new FlowableOnSubscribe<DynamicRealm>() {
@Override
public void subscribe(final FlowableEmitter<DynamicRealm> emitter) throws Exception {
// Instance is cached by Realm, so no need to keep strong reference
final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig);
final RealmChangeListener<DynamicRealm> listener = new RealmChangeListener<DynamicRealm>() {
@Override
public void onChange(DynamicRealm realm) {
if (!emitter.isCancelled()) {
emitter.onNext(returnFrozenObjects ? realm.freeze() : realm);
}
}
};
observableRealm.addChangeListener(listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
observableRealm.removeChangeListener(listener);
observableRealm.close();
}
}
}));
// Emit current value immediately
emitter.onNext(returnFrozenObjects ? observableRealm.freeze() : observableRealm);
}
}, BACK_PRESSURE_STRATEGY).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Aggregations