use of io.realm.entities.Owner in project realm-java by realm.
the class RealmListTests method removeAllFromRealm_outsideTransaction.
@Test
public void removeAllFromRealm_outsideTransaction() {
Owner owner = realm.where(Owner.class).findFirst();
RealmList<Dog> dogs = owner.getDogs();
try {
dogs.deleteAllFromRealm();
fail("removeAllFromRealm should be called in a transaction.");
} catch (IllegalStateException e) {
assertEquals("Changing Realm data can only be done from inside a write transaction.", e.getMessage());
}
}
use of io.realm.entities.Owner in project realm-java by realm.
the class RealmListTests method removeAllFromRealm.
@Test
public void removeAllFromRealm() {
Owner owner = realm.where(Owner.class).findFirst();
RealmList<Dog> dogs = owner.getDogs();
assertEquals(TEST_SIZE, dogs.size());
realm.beginTransaction();
dogs.deleteAllFromRealm();
realm.commitTransaction();
assertEquals(0, dogs.size());
assertEquals(0, realm.where(Dog.class).count());
}
use of io.realm.entities.Owner in project realm-java by realm.
the class OrderedCollectionChangeSetTests method reorderRealmList.
// Re-adds the dogs so they would be sorted by age in the list.
private void reorderRealmList(Realm realm) {
RealmResults<Dog> dogs = realm.where(Dog.class).findAllSorted(Dog.FIELD_AGE);
Owner owner = realm.where(Owner.class).findFirst();
owner.getDogs().clear();
for (Dog dog : dogs) {
owner.getDogs().add(dog);
}
}
use of io.realm.entities.Owner in project realm-java by realm.
the class BulkInsertTests method insertOrUpdate_cyclicDependenciesFromOtherRealm.
@Test
public void insertOrUpdate_cyclicDependenciesFromOtherRealm() {
RealmConfiguration config1 = configFactory.createConfiguration("realm1");
RealmConfiguration config2 = configFactory.createConfiguration("realm2");
Realm realm1 = Realm.getInstance(config1);
Realm realm2 = Realm.getInstance(config2);
realm1.beginTransaction();
Owner owner = realm1.createObject(Owner.class);
owner.setName("Kiba");
Dog dog = realm1.createObject(Dog.class);
dog.setName("Akamaru");
owner.getDogs().add(dog);
dog.setOwner(owner);
realm1.commitTransaction();
// Copies object with relations from realm1 to realm2.
realm2.beginTransaction();
realm2.insertOrUpdate(owner);
realm2.commitTransaction();
assertEquals(1, realm1.where(Owner.class).count());
assertEquals(1, realm1.where(Owner.class).findFirst().getDogs().size());
assertEquals(1, realm1.where(Dog.class).count());
assertEquals(realm1.where(Owner.class).count(), realm2.where(Owner.class).count());
assertEquals(realm1.where(Dog.class).count(), realm2.where(Dog.class).count());
assertEquals(1, realm2.where(Owner.class).findFirst().getDogs().size());
assertEquals(realm1.where(Owner.class).findFirst().getName(), realm2.where(Owner.class).findFirst().getName());
assertEquals(realm1.where(Owner.class).findFirst().getDogs().first().getName(), realm2.where(Owner.class).findFirst().getDogs().first().getName());
realm1.close();
realm2.close();
}
use of io.realm.entities.Owner in project realm-java by realm.
the class RealmAsyncQueryTests method queryingLinkHandover.
// This test makes sure that Async queries update when using link.
@Test
@RunTestInLooperThread
public void queryingLinkHandover() throws Throwable {
final AtomicInteger numberOfInvocations = new AtomicInteger(0);
final Realm realm = looperThread.getRealm();
final RealmResults<Dog> allAsync = realm.where(Dog.class).equalTo("owner.name", "kiba").findAllAsync();
looperThread.keepStrongReference(allAsync);
allAsync.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> object) {
switch(numberOfInvocations.incrementAndGet()) {
case 1:
assertEquals(0, allAsync.size());
assertTrue(allAsync.isLoaded());
assertTrue(allAsync.isValid());
assertTrue(allAsync.isEmpty());
new RealmBackgroundTask(realm.getConfiguration()) {
@Override
public void doInBackground(Realm realm) {
realm.beginTransaction();
Dog dog = realm.createObject(Dog.class);
dog.setAge(10);
dog.setName("Akamaru");
Owner kiba = realm.createObject(Owner.class);
kiba.setName("kiba");
dog.setOwner(kiba);
realm.commitTransaction();
}
}.awaitOrFail();
break;
case 2:
assertEquals(1, realm.where(Dog.class).count());
assertEquals(1, realm.where(Owner.class).count());
assertEquals(1, allAsync.size());
assertTrue(allAsync.isLoaded());
assertTrue(allAsync.isValid());
assertFalse(allAsync.isEmpty());
assertEquals(1, allAsync.size());
assertEquals("Akamaru", allAsync.get(0).getName());
assertEquals("kiba", allAsync.get(0).getOwner().getName());
looperThread.testComplete();
break;
}
}
});
}
Aggregations