use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmAsyncQueryTests method findFirstAsync_initalEmptyRow.
@Test
@RunTestInLooperThread
public void findFirstAsync_initalEmptyRow() throws Throwable {
Realm realm = looperThread.realm;
final AllTypes firstAsync = realm.where(AllTypes.class).findFirstAsync();
looperThread.keepStrongReference.add(firstAsync);
firstAsync.addChangeListener(new RealmChangeListener<AllTypes>() {
@Override
public void onChange(AllTypes object) {
assertTrue(firstAsync.load());
assertTrue(firstAsync.isLoaded());
assertTrue(firstAsync.isValid());
assertEquals(0, firstAsync.getColumnLong());
looperThread.testComplete();
}
});
realm.beginTransaction();
realm.createObject(AllTypes.class).setColumnLong(0);
realm.commitTransaction();
assertTrue(firstAsync.load());
assertTrue(firstAsync.isLoaded());
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmAsyncQueryTests method batchUpdateDifferentTypeOfQueries.
@Test
@RunTestInLooperThread
public void batchUpdateDifferentTypeOfQueries() {
final Realm realm = looperThread.realm;
realm.beginTransaction();
for (int i = 0; i < 5; ) {
AllTypes allTypes = realm.createObject(AllTypes.class);
allTypes.setColumnLong(i);
allTypes.setColumnString("data " + i % 3);
allTypes = realm.createObject(AllTypes.class);
allTypes.setColumnLong(i);
allTypes.setColumnString("data " + (++i % 3));
}
final long numberOfBlocks = 25;
// Must be greater than 1
final long numberOfObjects = 10;
realm.commitTransaction();
populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
RealmResults<AllTypes> findAllAsync = realm.where(AllTypes.class).findAllAsync();
RealmResults<AllTypes> findAllSorted = realm.where(AllTypes.class).findAllSortedAsync("columnString", Sort.ASCENDING);
RealmResults<AllTypes> findAllSortedMulti = realm.where(AllTypes.class).findAllSortedAsync(new String[] { "columnString", "columnLong" }, new Sort[] { Sort.ASCENDING, Sort.DESCENDING });
RealmResults<AnnotationIndexTypes> findDistinct = realm.where(AnnotationIndexTypes.class).distinctAsync("indexString");
looperThread.keepStrongReference.add(findAllAsync);
looperThread.keepStrongReference.add(findAllSorted);
looperThread.keepStrongReference.add(findAllSortedMulti);
looperThread.keepStrongReference.add(findDistinct);
final CountDownLatch queriesCompleted = new CountDownLatch(4);
final CountDownLatch bgRealmClosedLatch = new CountDownLatch(1);
final AtomicInteger batchUpdateCompleted = new AtomicInteger(0);
final AtomicInteger findAllAsyncInvocation = new AtomicInteger(0);
final AtomicInteger findAllSortedInvocation = new AtomicInteger(0);
final AtomicInteger findAllSortedMultiInvocation = new AtomicInteger(0);
final AtomicInteger findDistinctInvocation = new AtomicInteger(0);
findAllAsync.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
switch(findAllAsyncInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
findAllSorted.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
switch(findAllSortedInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
findAllSortedMulti.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
switch(findAllSortedMultiInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
findDistinct.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {
@Override
public void onChange(RealmResults<AnnotationIndexTypes> object) {
switch(findDistinctInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
// Waits for the queries to complete then sends a commit from
// another thread to trigger a batch update of the 4 queries.
new Thread() {
@Override
public void run() {
try {
queriesCompleted.await();
Realm bgRealm = Realm.getInstance(realm.getConfiguration());
bgRealm.beginTransaction();
bgRealm.createObject(AllTypes.class);
bgRealm.createObject(AnnotationIndexTypes.class);
bgRealm.commitTransaction();
bgRealm.close();
bgRealmClosedLatch.countDown();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
}.start();
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class NotificationsTest method realmObjectListenerAddedAfterCommit.
// TODO: Fix or delete this test after integration of object notification from Object Store
@Test
@RunTestInLooperThread
@Ignore
public void realmObjectListenerAddedAfterCommit() {
Realm realm = looperThread.realm;
realm.beginTransaction();
AllTypes obj = realm.createObject(AllTypes.class);
realm.commitTransaction();
realm.beginTransaction();
obj.setColumnLong(42);
realm.commitTransaction();
obj.addChangeListener(new RealmChangeListener<AllTypes>() {
@Override
public void onChange(AllTypes object) {
looperThread.testComplete();
}
});
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class NotificationsTest method listenersNotAllowedOnNonLooperThreads.
@Test
public void listenersNotAllowedOnNonLooperThreads() {
realm = Realm.getInstance(realmConfig);
realm.beginTransaction();
AllTypes obj = realm.createObject(AllTypes.class);
realm.commitTransaction();
RealmResults<AllTypes> results = realm.where(AllTypes.class).findAll();
// Global listener
try {
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm element) {
}
});
fail();
} catch (IllegalStateException ignored) {
}
// RealmResults listener
try {
results.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> element) {
}
});
fail();
} catch (IllegalStateException ignored) {
}
// Object listener
try {
obj.addChangeListener(new RealmChangeListener<RealmModel>() {
@Override
public void onChange(RealmModel element) {
}
});
fail();
} catch (IllegalStateException ignored) {
}
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_emptyChildObjectList.
@Test
public void createObjectFromJson_emptyChildObjectList() throws JSONException {
JSONObject allTypesObject = new JSONObject();
JSONArray dogList = new JSONArray();
allTypesObject.put("columnRealmList", dogList);
realm.beginTransaction();
realm.createObjectFromJson(AllTypes.class, allTypesObject);
realm.commitTransaction();
AllTypes obj = realm.where(AllTypes.class).findFirst();
assertEquals(0, obj.getColumnRealmList().size());
}
Aggregations