Search in sources :

Example 11 with DynamicRealm

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

Aggregations

DynamicRealm (io.realm.DynamicRealm)11 RealmConfiguration (io.realm.RealmConfiguration)10 Scheduler (io.reactivex.Scheduler)7 DynamicRealmObject (io.realm.DynamicRealmObject)5 RealmChangeListener (io.realm.RealmChangeListener)4 RealmObjectSchema (io.realm.RealmObjectSchema)4 RealmMigration (io.realm.RealmMigration)3 RealmResults (io.realm.RealmResults)3 RealmSchema (io.realm.RealmSchema)3 OrderedCollectionChangeSet (io.realm.OrderedCollectionChangeSet)2 OrderedRealmCollectionChangeListener (io.realm.OrderedRealmCollectionChangeListener)2 RealmList (io.realm.RealmList)2 HDWallet (com.toshi.crypto.HDWallet)1 Attachment (com.xabber.android.data.database.messagerealm.Attachment)1 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)1 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1 SyncInfo (com.xabber.android.data.database.messagerealm.SyncInfo)1 FieldAttribute (io.realm.FieldAttribute)1 ObjectChangeSet (io.realm.ObjectChangeSet)1 RealmObjectChangeListener (io.realm.RealmObjectChangeListener)1