Search in sources :

Example 1 with RealmObjectChangeListener

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);
}
Also used : DynamicRealmObject(io.realm.DynamicRealmObject) RealmObjectChangeListener(io.realm.RealmObjectChangeListener) Scheduler(io.reactivex.Scheduler) DynamicRealm(io.realm.DynamicRealm) ObjectChangeSet(io.realm.ObjectChangeSet) RealmConfiguration(io.realm.RealmConfiguration)

Example 2 with RealmObjectChangeListener

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);
}
Also used : RealmObjectChangeListener(io.realm.RealmObjectChangeListener) Scheduler(io.reactivex.Scheduler) ObjectChangeSet(io.realm.ObjectChangeSet) RealmConfiguration(io.realm.RealmConfiguration) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm)

Aggregations

Scheduler (io.reactivex.Scheduler)2 DynamicRealm (io.realm.DynamicRealm)2 ObjectChangeSet (io.realm.ObjectChangeSet)2 RealmConfiguration (io.realm.RealmConfiguration)2 RealmObjectChangeListener (io.realm.RealmObjectChangeListener)2 DynamicRealmObject (io.realm.DynamicRealmObject)1 Realm (io.realm.Realm)1