Search in sources :

Example 21 with RealmConfiguration

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

the class JNIColumnInfoTest method setUp.

@Before
public void setUp() {
    Realm.init(InstrumentationRegistry.getInstrumentation().getContext());
    RealmConfiguration config = configFactory.createConfiguration();
    sharedRealm = OsSharedRealm.getInstance(config, OsSharedRealm.VersionID.LIVE);
    table = TestHelper.createTable(sharedRealm, "temp", new TestHelper.AdditionalTableSetup() {

        @Override
        public void execute(Table table) {
            table.addColumn(RealmFieldType.STRING, "firstName");
            table.addColumn(RealmFieldType.STRING, "lastName");
        }
    });
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Before(org.junit.Before)

Example 22 with RealmConfiguration

use of io.realm.RealmConfiguration 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 23 with RealmConfiguration

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

the class RealmObservableFactory method from.

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

        @Override
        public void subscribe(final FlowableEmitter<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 DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig);
            resultsRefs.get().acquireReference(results);
            final RealmChangeListener<RealmResults<E>> listener = new RealmChangeListener<RealmResults<E>>() {

                @Override
                public void onChange(RealmResults<E> results) {
                    if (!emitter.isCancelled()) {
                        emitter.onNext(returnFrozenObjects ? results.freeze() : results);
                    }
                }
            };
            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(returnFrozenObjects ? results.freeze() : results);
        }
    }, 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) RealmResults(io.realm.RealmResults)

Example 24 with RealmConfiguration

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

the class RealmObservableFactory method from.

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

        @Override
        public void subscribe(final FlowableEmitter<Realm> emitter) throws Exception {
            // Instance is cached by Realm, so no need to keep strong reference
            final Realm observableRealm = Realm.getInstance(realmConfig);
            final RealmChangeListener<Realm> listener = new RealmChangeListener<Realm>() {

                @Override
                public void onChange(Realm 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) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm)

Example 25 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(DynamicRealm 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 DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig);
            resultsRefs.get().acquireReference(results);
            final OrderedRealmCollectionChangeListener<RealmResults<E>> listener = new OrderedRealmCollectionChangeListener<RealmResults<E>>() {

                @Override
                public void onChange(RealmResults<E> results, OrderedCollectionChangeSet changeSet) {
                    if (!emitter.isDisposed()) {
                        emitter.onNext(new CollectionChange<>(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) DynamicRealm(io.realm.DynamicRealm) RealmConfiguration(io.realm.RealmConfiguration) OrderedRealmCollectionChangeListener(io.realm.OrderedRealmCollectionChangeListener) 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