Search in sources :

Example 51 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class RealmObservableFactory method changesetsFrom.

@Override
public <E> Observable<CollectionChange<RealmList<E>>> changesetsFrom(DynamicRealm realm, final RealmList<E> list) {
    if (realm.isFrozen()) {
        return Observable.just(new CollectionChange<RealmList<E>>(list, null));
    }
    final RealmConfiguration realmConfig = realm.getConfiguration();
    Scheduler scheduler = getScheduler();
    return Observable.create(new ObservableOnSubscribe<CollectionChange<RealmList<E>>>() {

        @Override
        public void subscribe(final ObservableEmitter<CollectionChange<RealmList<E>>> emitter) {
            // If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
            if (!list.isValid())
                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);
            listRefs.get().acquireReference(list);
            final OrderedRealmCollectionChangeListener<RealmList<E>> listener = new OrderedRealmCollectionChangeListener<RealmList<E>>() {

                @Override
                public void onChange(RealmList<E> list, OrderedCollectionChangeSet changeSet) {
                    if (!list.isValid()) {
                        // Terminate stream if list is no longer valid. This indicates
                        // the parent object has been deleted, so no further updates
                        // will be coming.
                        emitter.onComplete();
                        return;
                    }
                    if (!emitter.isDisposed()) {
                        emitter.onNext(new CollectionChange<>(returnFrozenObjects ? list.freeze() : list, changeSet));
                    }
                }
            };
            list.addChangeListener(listener);
            // Cleanup when stream is disposed
            emitter.setDisposable(Disposables.fromRunnable(new Runnable() {

                @Override
                public void run() {
                    if (!observableRealm.isClosed()) {
                        list.removeChangeListener(listener);
                        observableRealm.close();
                    }
                    listRefs.get().releaseReference(list);
                }
            }));
            // Emit current value immediately
            emitter.onNext(new CollectionChange<>(returnFrozenObjects ? list.freeze() : list, null));
        }
    }).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Also used : RealmList(io.realm.RealmList) OrderedCollectionChangeSet(io.realm.OrderedCollectionChangeSet) Scheduler(io.reactivex.Scheduler) DynamicRealm(io.realm.DynamicRealm) RealmConfiguration(io.realm.RealmConfiguration) OrderedRealmCollectionChangeListener(io.realm.OrderedRealmCollectionChangeListener)

Example 52 with RealmConfiguration

use of io.realm.RealmConfiguration 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);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) RealmChangeListener(io.realm.RealmChangeListener) Scheduler(io.reactivex.Scheduler) DynamicRealm(io.realm.DynamicRealm)

Example 53 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class RealmObservableFactory method changesetsFrom.

@Override
public <E> Observable<CollectionChange<RealmList<E>>> changesetsFrom(Realm realm, final RealmList<E> list) {
    if (realm.isFrozen()) {
        return Observable.just(new CollectionChange<RealmList<E>>(list, null));
    }
    final RealmConfiguration realmConfig = realm.getConfiguration();
    Scheduler scheduler = getScheduler();
    return Observable.create(new ObservableOnSubscribe<CollectionChange<RealmList<E>>>() {

        @Override
        public void subscribe(final ObservableEmitter<CollectionChange<RealmList<E>>> emitter) {
            // If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
            if (!list.isValid())
                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);
            listRefs.get().acquireReference(list);
            final OrderedRealmCollectionChangeListener<RealmList<E>> listener = new OrderedRealmCollectionChangeListener<RealmList<E>>() {

                @Override
                public void onChange(RealmList<E> list, OrderedCollectionChangeSet changeSet) {
                    if (!list.isValid()) {
                        // Terminate stream if list is no longer valid. This indicates
                        // the parent object has been deleted, so no further updates
                        // will be coming.
                        emitter.onComplete();
                        return;
                    }
                    if (!emitter.isDisposed()) {
                        emitter.onNext(new CollectionChange<>(returnFrozenObjects ? list.freeze() : list, changeSet));
                    }
                }
            };
            list.addChangeListener(listener);
            // Cleanup when stream is disposed
            emitter.setDisposable(Disposables.fromRunnable(new Runnable() {

                @Override
                public void run() {
                    if (!observableRealm.isClosed()) {
                        list.removeChangeListener(listener);
                        observableRealm.close();
                    }
                    listRefs.get().releaseReference(list);
                }
            }));
            // Emit current value immediately
            emitter.onNext(new CollectionChange<>(returnFrozenObjects ? list.freeze() : list, null));
        }
    }).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Also used : RealmList(io.realm.RealmList) OrderedCollectionChangeSet(io.realm.OrderedCollectionChangeSet) Scheduler(io.reactivex.Scheduler) RealmConfiguration(io.realm.RealmConfiguration) OrderedRealmCollectionChangeListener(io.realm.OrderedRealmCollectionChangeListener) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm)

Example 54 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class RealmObservableFactory method from.

@Override
public <E> Flowable<RealmList<E>> from(Realm realm, final RealmList<E> list) {
    if (realm.isFrozen()) {
        return Flowable.just(list);
    }
    final RealmConfiguration realmConfig = realm.getConfiguration();
    Scheduler scheduler = getScheduler();
    return Flowable.create(new FlowableOnSubscribe<RealmList<E>>() {

        @Override
        public void subscribe(final FlowableEmitter<RealmList<E>> emitter) {
            // If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
            if (!list.isValid())
                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);
            listRefs.get().acquireReference(list);
            final RealmChangeListener<RealmList<E>> listener = new RealmChangeListener<RealmList<E>>() {

                @Override
                public void onChange(RealmList<E> list) {
                    if (!list.isValid()) {
                        // Terminate stream if list is no longer valid. This indicates
                        // the parent object has been deleted, so no further updates
                        // will be coming.
                        emitter.onComplete();
                        return;
                    }
                    if (!emitter.isCancelled()) {
                        emitter.onNext(returnFrozenObjects ? list.freeze() : list);
                    }
                }
            };
            list.addChangeListener(listener);
            // Cleanup when stream is disposed
            emitter.setDisposable(Disposables.fromRunnable(new Runnable() {

                @Override
                public void run() {
                    if (!observableRealm.isClosed()) {
                        list.removeChangeListener(listener);
                        observableRealm.close();
                    }
                    listRefs.get().releaseReference(list);
                }
            }));
            // Emit current value immediately
            emitter.onNext(returnFrozenObjects ? list.freeze() : list);
        }
    }, BACK_PRESSURE_STRATEGY).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) RealmChangeListener(io.realm.RealmChangeListener) RealmList(io.realm.RealmList) Scheduler(io.reactivex.Scheduler) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm)

Example 55 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class RealmObservableFactory method changesetsFrom.

@Override
public <E> Observable<CollectionChange<RealmResults<E>>> changesetsFrom(Realm realm, final RealmResults<E> results) {
    if (realm.isFrozen()) {
        return Observable.just(new CollectionChange<RealmResults<E>>(results, null));
    }
    final RealmConfiguration realmConfig = realm.getConfiguration();
    Scheduler scheduler = getScheduler();
    return Observable.create(new ObservableOnSubscribe<CollectionChange<RealmResults<E>>>() {

        @Override
        public void subscribe(final ObservableEmitter<CollectionChange<RealmResults<E>>> emitter) {
            // If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
            if (!results.isValid())
                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);
            resultsRefs.get().acquireReference(results);
            final OrderedRealmCollectionChangeListener<RealmResults<E>> listener = new OrderedRealmCollectionChangeListener<RealmResults<E>>() {

                @Override
                public void onChange(RealmResults<E> e, OrderedCollectionChangeSet changeSet) {
                    if (!emitter.isDisposed()) {
                        emitter.onNext(new CollectionChange<RealmResults<E>>(returnFrozenObjects ? results.freeze() : results, changeSet));
                    }
                }
            };
            results.addChangeListener(listener);
            // Cleanup when stream is disposed
            emitter.setDisposable(Disposables.fromRunnable(new Runnable() {

                @Override
                public void run() {
                    if (!observableRealm.isClosed()) {
                        results.removeChangeListener(listener);
                        observableRealm.close();
                    }
                    resultsRefs.get().releaseReference(results);
                }
            }));
            // Emit current value immediately
            emitter.onNext(new CollectionChange<>(returnFrozenObjects ? results.freeze() : results, null));
        }
    }).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Also used : OrderedCollectionChangeSet(io.realm.OrderedCollectionChangeSet) Scheduler(io.reactivex.Scheduler) RealmConfiguration(io.realm.RealmConfiguration) OrderedRealmCollectionChangeListener(io.realm.OrderedRealmCollectionChangeListener) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm) RealmResults(io.realm.RealmResults)

Aggregations

RealmConfiguration (io.realm.RealmConfiguration)64 Realm (io.realm.Realm)20 DynamicRealm (io.realm.DynamicRealm)17 Scheduler (io.reactivex.Scheduler)14 RealmChangeListener (io.realm.RealmChangeListener)8 BeforeExperiment (dk.ilios.spanner.BeforeExperiment)6 Before (org.junit.Before)5 DynamicRealmObject (io.realm.DynamicRealmObject)4 RealmList (io.realm.RealmList)4 RealmResults (io.realm.RealmResults)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Context (android.content.Context)3 OrderedCollectionChangeSet (io.realm.OrderedCollectionChangeSet)3 OrderedRealmCollectionChangeListener (io.realm.OrderedRealmCollectionChangeListener)3 RealmObjectSchema (io.realm.RealmObjectSchema)3 AllTypes (io.realm.entities.AllTypes)3 IntentFilter (android.content.IntentFilter)2 LinearLayout (android.widget.LinearLayout)2 RealmSchema (io.realm.RealmSchema)2