use of io.realm.RealmObjectChangeListener in project realm-java by realm.
the class RealmObservableFactory method changesetsFrom.
@Override
public Observable<ObjectChange<DynamicRealmObject>> changesetsFrom(DynamicRealm realm, final DynamicRealmObject object) {
if (realm.isFrozen()) {
return Observable.just(new ObjectChange<DynamicRealmObject>(object, null));
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Observable.create(new ObservableOnSubscribe<ObjectChange<DynamicRealmObject>>() {
@Override
public void subscribe(final ObservableEmitter<ObjectChange<DynamicRealmObject>> emitter) {
// If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
if (!RealmObject.isValid(object))
return;
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig);
objectRefs.get().acquireReference(object);
final RealmObjectChangeListener<DynamicRealmObject> listener = new RealmObjectChangeListener<DynamicRealmObject>() {
@Override
public void onChange(DynamicRealmObject obj, ObjectChangeSet changeSet) {
if (!emitter.isDisposed()) {
emitter.onNext(new ObjectChange<>(returnFrozenObjects ? RealmObject.freeze(obj) : obj, changeSet));
}
}
};
object.addChangeListener(listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
RealmObject.removeChangeListener(object, listener);
observableRealm.close();
}
objectRefs.get().releaseReference(object);
}
}));
// Emit current value immediately
emitter.onNext(new ObjectChange<>(returnFrozenObjects ? RealmObject.freeze(object) : object, null));
}
}).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
use of io.realm.RealmObjectChangeListener in project realm-java by realm.
the class RealmObservableFactory method changesetsFrom.
@Override
public <E extends RealmModel> Observable<ObjectChange<E>> changesetsFrom(Realm realm, final E object) {
if (realm.isFrozen()) {
return Observable.just(new ObjectChange<E>(object, null));
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Observable.create(new ObservableOnSubscribe<ObjectChange<E>>() {
@Override
public void subscribe(final ObservableEmitter<ObjectChange<E>> emitter) {
// If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
if (!RealmObject.isValid(object))
return;
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final Realm observableRealm = Realm.getInstance(realmConfig);
objectRefs.get().acquireReference(object);
final RealmObjectChangeListener<E> listener = new RealmObjectChangeListener<E>() {
@Override
public void onChange(E obj, ObjectChangeSet changeSet) {
if (!emitter.isDisposed()) {
emitter.onNext(new ObjectChange<>(returnFrozenObjects ? RealmObject.freeze(obj) : obj, changeSet));
}
}
};
RealmObject.addChangeListener(object, listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
RealmObject.removeChangeListener(object, listener);
observableRealm.close();
}
objectRefs.get().releaseReference(object);
}
}));
// Emit current value immediately
emitter.onNext(new ObjectChange<>(returnFrozenObjects ? RealmObject.freeze(object) : object, null));
}
}).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Aggregations