Search in sources :

Example 6 with DynamicRealmObject

use of io.realm.DynamicRealmObject in project ocreader by schaal.

the class DatabaseMigration method migrate.

@Override
public void migrate(@NonNull final DynamicRealm realm, long oldVersion, long newVersion) {
    Log.d(TAG, "Starting migration from " + oldVersion + " to " + newVersion);
    // contentHash for items
    if (oldVersion < 9) {
        throw new IllegalStateException("Migration from Schema < 9 not supported");
    }
    RealmSchema schema = realm.getSchema();
    /*
          9 -> 10

          - Add primary key ID to TemporaryFeed
          - Rename TemporaryFeed id to treeItemId
          - add TemporaryFeed object for list and pager activities
         */
    if (oldVersion == 9) {
        realm.delete("TemporaryFeed");
        final RealmObjectSchema temporaryFeedSchema = schema.get("TemporaryFeed");
        if (temporaryFeedSchema == null)
            throw new IllegalStateException("TemporaryFeed schema not found");
        temporaryFeedSchema.renameField(TemporaryFeed.ID, TemporaryFeed.TREE_ITEM_ID).addField(TemporaryFeed.ID, long.class, FieldAttribute.PRIMARY_KEY);
        realm.createObject("TemporaryFeed", TemporaryFeed.LIST_ID);
        realm.createObject("TemporaryFeed", TemporaryFeed.PAGER_ID);
        oldVersion++;
    }
    /*
          10 -> 11

           - Make sure every item has updatedAt != null, set updatedAt = pubDate if not
         */
    if (oldVersion == 10) {
        for (DynamicRealmObject object : realm.where("Item").isNull(Item.UPDATED_AT).findAll()) {
            object.setDate(Item.UPDATED_AT, object.getDate(Item.PUB_DATE));
        }
        oldVersion++;
    }
    /*
          11 -> 12

          - Add active property to Item
         */
    if (oldVersion == 11) {
        final RealmObjectSchema itemSchema = schema.get("Item");
        if (itemSchema == null)
            throw new IllegalStateException("Item schema not found");
        itemSchema.addField(Item.ACTIVE, boolean.class, FieldAttribute.INDEXED);
        // noinspection UnusedAssignment
        oldVersion++;
    }
}
Also used : DynamicRealmObject(io.realm.DynamicRealmObject) RealmSchema(io.realm.RealmSchema) RealmObjectSchema(io.realm.RealmObjectSchema)

Example 7 with DynamicRealmObject

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

Aggregations

DynamicRealmObject (io.realm.DynamicRealmObject)7 DynamicRealm (io.realm.DynamicRealm)5 RealmObjectSchema (io.realm.RealmObjectSchema)5 RealmSchema (io.realm.RealmSchema)5 RealmConfiguration (io.realm.RealmConfiguration)4 Scheduler (io.reactivex.Scheduler)2 RealmMigration (io.realm.RealmMigration)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 RealmChangeListener (io.realm.RealmChangeListener)1 RealmObjectChangeListener (io.realm.RealmObjectChangeListener)1 RealmResults (io.realm.RealmResults)1 File (java.io.File)1 List (java.util.List)1 UUID (java.util.UUID)1