Search in sources :

Example 6 with DynamicRealm

use of io.realm.DynamicRealm 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)

Example 7 with DynamicRealm

use of io.realm.DynamicRealm in project CustomViews by AndroidStudy233.

the class RealmManager method init.

/**
 * 初始化realm,比如说如果你的app在不同的账号登录同一台设备就要需要建立不同的数据库(根据名称来区分)
 *
 * @param realmName 数据库名称
 * @param version   版本号
 */
private static void init(String realmName, int version) {
    RealmConfiguration configuration = new RealmConfiguration.Builder().name(// 配置名字
    realmName).encryptionKey(// 加密用字段,不是64位会报错
    new byte[64]).schemaVersion(// 版本号
    version).migration(new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            MyLogUtil.debug("oldVersion: " + oldVersion);
            MyLogUtil.debug("newVersion: " + newVersion);
            RealmObjectSchema schema = realm.getSchema().get("Doctor");
            Set<String> names = schema.getFieldNames();
            if (names.contains("id"))
                schema.removeField("id");
            if (names.contains("name") && !schema.getPrimaryKey().equals("name")) {
                schema.removePrimaryKey();
                schema.addPrimaryKey("name");
            }
        }
    }).build();
    Realm.setDefaultConfiguration(configuration);
// Context.getFilesDir() 目录下的realmName.realm数据库
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) DynamicRealm(io.realm.DynamicRealm) RealmObjectSchema(io.realm.RealmObjectSchema) RealmMigration(io.realm.RealmMigration)

Example 8 with DynamicRealm

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

the class RealmObservableFactory method from.

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

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

                @Override
                public void onChange(DynamicRealmObject obj) {
                    if (!emitter.isCancelled()) {
                        emitter.onNext(returnFrozenObjects ? RealmObject.freeze(obj) : obj);
                    }
                }
            };
            RealmObject.addChangeListener(object, 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(returnFrozenObjects ? RealmObject.freeze(object) : object);
        }
    }, BACK_PRESSURE_STRATEGY).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) DynamicRealmObject(io.realm.DynamicRealmObject) RealmChangeListener(io.realm.RealmChangeListener) Scheduler(io.reactivex.Scheduler) DynamicRealm(io.realm.DynamicRealm)

Example 9 with DynamicRealm

use of io.realm.DynamicRealm 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)

Example 10 with DynamicRealm

use of io.realm.DynamicRealm 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)

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