本文整理匯總了Java中io.realm.DynamicRealm類的典型用法代碼示例。如果您正苦於以下問題:Java DynamicRealm類的具體用法?Java DynamicRealm怎麽用?Java DynamicRealm使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DynamicRealm類屬於io.realm包,在下文中一共展示了DynamicRealm類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: removingPrimaryKeyRemovesConstraint_typeSetters
import io.realm.DynamicRealm; //導入依賴的package包/類
/**
* This test surfaces a bunch of problems, most of them seem to be around caching of the schema
* during a transaction
*
* 1) Removing the primary key do not invalidate the cache in RealmSchema and those cached
* are ImmutableRealmObjectSchema so do not change when the primary key is removed.
*
* 2) Addding `schema.refresh()` to RealmObjectSchema.removePrimaryKey()` causes
* RealmPrimaryKeyConstraintException anyway. Unclear why.
*/
@Test
public void removingPrimaryKeyRemovesConstraint_typeSetters() {
RealmConfiguration config = configFactory.createConfigurationBuilder()
.name("removeConstraints").build();
DynamicRealm realm = DynamicRealm.getInstance(config);
RealmSchema realmSchema = realm.getSchema();
realm.beginTransaction();
RealmObjectSchema tableSchema = realmSchema.create("Employee")
.addField("name", String.class, FieldAttribute.PRIMARY_KEY);
realm.createObject("Employee", "Foo");
DynamicRealmObject obj = realm.createObject("Employee", "Foo2");
try {
// Tries to create 2nd entry with name Foo.
obj.setString("name", "Foo");
} catch (IllegalArgumentException e) {
tableSchema.removePrimaryKey();
obj.setString("name", "Foo");
} finally {
realm.close();
}
}
示例2: onCreate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder().schemaVersion(7) //
.migration(new RealmMigration() {
@Override
public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
RealmAutoMigration.migrate(realm);
}
}) //
.initialData(realm -> {
Cat cat = new Cat();
for(CatNames catName : CatNames.values()) {
cat.setName(catName.getName());
realm.insert(cat);
}
}) //
.build());
SingletonComponent singletonComponent = DaggerSingletonComponent.create();
Injector.setComponent(singletonComponent);
}
示例3: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// During a migration, a DynamicRealm is exposed. A DynamicRealm is an untyped variant of a normal Realm, but
// with the same object creation and query capabilities.
// A DynamicRealm uses Strings instead of Class references because the Classes might not even exist or have been
// renamed.
// Access the Realm schema in order to create, modify or delete classes and their fields.
RealmSchema schema = realm.getSchema();
// Migrate from version 0 to version 1
if (oldVersion == 0) {
oldVersion++;
}
// Migrate from version 1 to version 2
if (oldVersion == 1) {
RealmObjectSchema bloodGlucoseDataSchema = schema.get("BloodGlucoseData");
bloodGlucoseDataSchema
.addField("timezoneOffsetInMinutes", int.class)
.transform(timezoneTransformFunction);
//oldVersion++;
}
}
示例4: onCreate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override public void onCreate() {
super.onCreate();
sInstance = this;
RealmConfiguration configuration =
new RealmConfiguration.Builder(this).deleteRealmIfMigrationNeeded()
.migration(new RealmMigration() {
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
})
.name("chao.realm")
.build();
Realm.setDefaultConfiguration(configuration);
Stetho.initialize(Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
.build());
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
示例5: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
/*
* Migrates to version 1 of the schema.
* - Make uniqueId the primary key for the RBook class instead of the relative path.
*/
if (oldVersion == 0) {
schema.get("RBook")
.removePrimaryKey() // Remove @PrimaryKey from relPath.
.addIndex("relPath") // Add @Index to relPath.
.addPrimaryKey("uniqueId"); // Add @PrimaryKey to uniqueId.
oldVersion++;
}
}
示例6: onCreate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void onCreate() {
super.onCreate();
mAppContext = this;
// 配置Realm數據庫
RealmConfiguration configuration = new RealmConfiguration
.Builder(this)
.deleteRealmIfMigrationNeeded()
.schemaVersion(6)
.migration(new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
}).build();
Realm.setDefaultConfiguration(configuration);
//配置騰訊bugly
CrashReport.initCrashReport(getApplicationContext(), ConstantUtil.BUGLY_ID, false);
}
示例7: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// DynamicRealm exposes an editable schema
RealmSchema schema = realm.getSchema();
// Migrate to version 1: Add a new class.
// Example:
// public Person extends RealmObject {
// private String name;
// private int age;
// // getters and setters left out for brevity
// }
if (oldVersion == 0) {
schema.create("UserScript")
.addField("title", String.class)
.addField("url", String.class);
oldVersion++;
}
if (oldVersion == 1) {
schema.get("UserScript")
.addField("javascript", String.class);
oldVersion++;
}
}
示例8: onCreate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration config = new RealmConfiguration.Builder(getApplicationContext())
.name(getResources().getString(R.string.database_conf_name))
.schemaVersion(getResources().getInteger(R.integer.database_conf_version))
.modules(new DbModule())
.deleteRealmIfMigrationNeeded()
.migration(new RealmMigration() {
@Override
public void migrate(DynamicRealm dynamicRealm, long l, long l1) {
Log.d(TAG, "migrate l [" + l + "] l1 [" + l1 + "]");
}
})
.build();
Realm.setDefaultConfiguration(config);
}
示例9: from
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public <E> Flowable<RealmList<E>> from(DynamicRealm realm, final RealmList<E> list) {
final RealmConfiguration realmConfig = realm.getConfiguration();
return Flowable.create(new FlowableOnSubscribe<RealmList<E>>() {
@Override
public void subscribe(final FlowableEmitter<RealmList<E>> emitter) throws Exception {
// 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> results) {
if (!emitter.isCancelled()) {
emitter.onNext(list);
}
}
};
list.addChangeListener(listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
list.removeChangeListener(listener);
observableRealm.close();
listRefs.get().releaseReference(list);
}
}));
// Emit current value immediately
emitter.onNext(list);
}
}, BACK_PRESSURE_STRATEGY);
}
示例10: changesetsFrom
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public <E> Observable<CollectionChange<RealmList<E>>> changesetsFrom(DynamicRealm realm, final RealmList<E> list) {
final RealmConfiguration realmConfig = realm.getConfiguration();
return Observable.create(new ObservableOnSubscribe<CollectionChange<RealmList<E>>>() {
@Override
public void subscribe(final ObservableEmitter<CollectionChange<RealmList<E>>> emitter) throws Exception {
// 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> results, OrderedCollectionChangeSet changeSet) {
if (!emitter.isDisposed()) {
emitter.onNext(new CollectionChange<>(results, changeSet));
}
}
};
list.addChangeListener(listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
list.removeChangeListener(listener);
observableRealm.close();
listRefs.get().releaseReference(list);
}
}));
// Emit current value immediately
emitter.onNext(new CollectionChange<>(list, null));
}
});
}
示例11: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@SuppressLint("DefaultLocale")
@Override
public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
if (oldVersion < newVersion) {
// Unknown migration
throw new RealmMigrationNeededException(realm.getPath(), String.format("Migration missing from v%d to v%d", oldVersion, newVersion));
}
}
示例12: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// DynamicRealm exposes an editable schema
RealmSchema schema = realm.getSchema();
// Migrate to version 1: Add a new class.
// Example:
// public Person extends RealmObject {
// private String name;
// private int age;
// // getters and setters left out for brevity
// }
if (oldVersion == 0) {
schema.get("RealmContact")
.addPrimaryKey("idx");
// .addField("idx", String.class, FieldAttribute.PRIMARY_KEY);
oldVersion++;
}
// Migrate to version 2: Add a primary key + object references
// Example:
// public Person extends RealmObject {
// private String name;
// @PrimaryKey
// private int age;
// private Dog favoriteDog;
// private RealmList<Dog> dogs;
// // getters and setters left out for brevity
// }
if (oldVersion == 1) {
/*schema.get("RealmContact")
.addField("idx", String.class, FieldAttribute.PRIMARY_KEY)
.addField("name", String.class)
.addField("phone", String.class);
oldVersion++;*/
}
}
示例13: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
Log.i(TAG, "MIGRATING DATABASE from v%d to v%d", oldVersion, newVersion);
// why < 2? because I forgot to assign a schema version until I wrote this migration, so the
// oldVersion here will be whatever default is assigned by Realm
if (oldVersion < 2) {
if (!schema.get("Post").hasField("customExcerpt")) {
Log.d(TAG, "ADDING CUSTOM EXCERPT FIELD TO POST TABLE");
schema.get("Post").addField("customExcerpt", String.class);
}
oldVersion = 2;
}
}
示例14: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
if (newVersion > oldVersion) {
RealmSchema realmSchema = realm.getSchema();
if (oldVersion == 1) {
realmSchema.create("UpgradeModel")
.addField("localId", String.class, FieldAttribute.PRIMARY_KEY)
.addField("version", String.class)
.addField("url", String.class)
.addField("compulsive_upgrade", String.class)
.addField("description", String.class);
}
updateLearningModel(realmSchema);
}
}
示例15: migrate
import io.realm.DynamicRealm; //導入依賴的package包/類
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion < 1) {
schema.get("Devotional").addField("unread", boolean.class);
}
}