Search in sources :

Example 36 with RealmList

use of io.realm.RealmList 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 37 with RealmList

use of io.realm.RealmList 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 38 with RealmList

use of io.realm.RealmList 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 39 with RealmList

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

the class RealmObservableFactory method from.

@Override
public <E> Flowable<RealmList<E>> from(DynamicRealm 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 DynamicRealm observableRealm = DynamicRealm.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) DynamicRealm(io.realm.DynamicRealm)

Aggregations

RealmList (io.realm.RealmList)39 Realm (io.realm.Realm)23 Rider (ch.hsr.sa.radiotour.dataaccess.models.Rider)17 Test (org.junit.Test)8 RaceGroup (ch.hsr.sa.radiotour.dataaccess.models.RaceGroup)7 Judgement (ch.hsr.sa.radiotour.dataaccess.models.Judgement)6 RiderStageConnection (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection)6 JudgmentRiderConnection (ch.hsr.sa.radiotour.dataaccess.models.JudgmentRiderConnection)4 Scheduler (io.reactivex.Scheduler)4 DynamicRealm (io.realm.DynamicRealm)4 RealmConfiguration (io.realm.RealmConfiguration)4 DialogInterface (android.content.DialogInterface)2 View (android.view.View)2 Maillot (ch.hsr.sa.radiotour.dataaccess.models.Maillot)2 Reward (ch.hsr.sa.radiotour.dataaccess.models.Reward)2 EmailRealm (com.xabber.android.data.database.realm.EmailRealm)2 SocialBindingRealm (com.xabber.android.data.database.realm.SocialBindingRealm)2 SyncStateRealm (com.xabber.android.data.database.realm.SyncStateRealm)2 XMPPUserRealm (com.xabber.android.data.database.realm.XMPPUserRealm)2 XabberAccountRealm (com.xabber.android.data.database.realm.XabberAccountRealm)2