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++;
}
}
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);
}
Aggregations