Search in sources :

Example 26 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(final Realm 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 Realm observableRealm = Realm.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) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm) RealmResults(io.realm.RealmResults)

Example 27 with RealmConfiguration

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

the class GridViewExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realm_example);
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
    // Clear the realm from last time
    Realm.deleteRealm(realmConfiguration);
    // Create a new empty instance of Realm
    realm = Realm.getInstance(realmConfiguration);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration)

Example 28 with RealmConfiguration

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

the class RealmAllocBenchmarks method before.

@BeforeExperiment
public void before() {
    Realm.init(InstrumentationRegistry.getTargetContext());
    RealmConfiguration config = new RealmConfiguration.Builder().build();
    Realm.deleteRealm(config);
    realm = Realm.getInstance(config);
    realm.beginTransaction();
    realm.createObject(AllTypes.class).getColumnRealmList().add(realm.createObject(Dog.class));
    realm.commitTransaction();
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Dog(io.realm.entities.Dog) BeforeExperiment(dk.ilios.spanner.BeforeExperiment)

Example 29 with RealmConfiguration

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

the class RealmObjectReadBenchmarks method before.

@BeforeExperiment
public void before() {
    Realm.init(InstrumentationRegistry.getTargetContext());
    RealmConfiguration config = new RealmConfiguration.Builder().build();
    Realm.deleteRealm(config);
    realm = Realm.getInstance(config);
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            readObject = realm.createObject(AllTypes.class);
            readObject.setColumnString("Foo");
            readObject.setColumnLong(42);
            readObject.setColumnDouble(1.234D);
        }
    });
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Realm(io.realm.Realm) BeforeExperiment(dk.ilios.spanner.BeforeExperiment)

Example 30 with RealmConfiguration

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

the class TestRealmConfigurationFactory method createConfiguration.

public RealmConfiguration createConfiguration(String subDir, String name, Object module, byte[] key) {
    RealmConfiguration.Builder builder = createConfigurationBuilder();
    File folder = getRoot();
    if (subDir != null) {
        folder = new File(folder, subDir);
        assertTrue(folder.mkdirs());
    }
    builder.directory(folder);
    if (name != null) {
        builder.name(name);
    }
    if (module != null) {
        builder.modules(module);
    }
    if (key != null) {
        builder.encryptionKey(key);
    }
    RealmConfiguration configuration = builder.build();
    configurations.add(configuration);
    return configuration;
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) File(java.io.File)

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