Search in sources :

Example 1 with RealmBackgroundTask

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;
            }
        }
    });
}
Also used : Owner(io.realm.entities.Owner) RealmBackgroundTask(io.realm.util.RealmBackgroundTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

Dog (io.realm.entities.Dog)1 Owner (io.realm.entities.Owner)1 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)1 RealmBackgroundTask (io.realm.util.RealmBackgroundTask)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1