use of io.realm.util.RealmBackgroundTask 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.realm;
final RealmResults<Dog> allAsync = realm.where(Dog.class).equalTo("owner.name", "kiba").findAllAsync();
looperThread.keepStrongReference.add(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