Source:
private void distinctAllFields(Realm realm, String prefix) { final long numberOfBlocks = 3; final long numberOfObjects = 3; populateForDistinctAllTypes(realm, numberOfBlocks, numberOfObjects); DynamicRealm dynamicRealm = DynamicRealm.createInstance(realm.sharedRealm); RealmResults<DynamicRealmObject> all = dynamicRealm.where(AllTypes.CLASS_NAME) .findAll(); HashSet types = new HashSet(Arrays.asList(RealmFieldType.values())); RealmObjectSchema schema = realm.getSchema().getSchemaForClass(AllTypes.CLASS_NAME); Set<String> fieldNames = schema.getFieldNames(); for (String fieldName : fieldNames) { String field = prefix + fieldName; RealmFieldType type = schema.getFieldType(fieldName); if (supportDistinct(type)) { RealmResults<AllTypes> distinct = realm.where(AllTypes.class) .distinct(fieldName) .findAll(); Set<List<? super Object>> values = distinct(all, field); assertEquals(field, values.size(), distinct.size()); switch (type) { case BOOLEAN: assertEquals(field, 2, distinct.size()); break; case OBJECT: if (fieldName.equals("columnRealmObject")) { assertEquals(field, numberOfBlocks, distinct.size()); } else if (fieldName.equals("columnRealmLink")){ assertEquals(field, numberOfBlocks * numberOfObjects, distinct.size()); } else { fail("Unknown object " + fieldName); } break; default: assertEquals(field, numberOfObjects, distinct.size()); break; } } else { try { realm.where(AllTypes.class) .distinct(fieldName) .findAll(); fail(); } catch (IllegalArgumentException ignore) { } } types.remove(type); } assertEquals(types.toString(), Sets.newSet(RealmFieldType.LINKING_OBJECTS), types); }

Target:
private void distinctAllFields(Realm realm, String prefix) { final long numberOfBlocks = 3; final long numberOfObjects = 3; populateForDistinctAllTypes(realm, numberOfBlocks, numberOfObjects); DynamicRealm dynamicRealm = DynamicRealm.createInstance(realm.sharedRealm); RealmResults<DynamicRealmObject> all = dynamicRealm.where(AllTypes.CLASS_NAME) .findAll(); HashSet types = new HashSet(Arrays.asList(RealmFieldType.values())); RealmObjectSchema schema = realm.getSchema().getSchemaForClass(AllTypes.CLASS_NAME); Set<String> fieldNames = schema.getFieldNames(); for (String fieldName : fieldNames) { String field = prefix + fieldName; RealmFieldType type = schema.getFieldType(fieldName); if (supportDistinct(type)) { RealmResults<AllTypes> distinct = realm.where(AllTypes.class) .distinct(field) .findAll(); Set<List<? super Object>> values = distinct(all, field); assertEquals(field, values.size(), distinct.size()); switch (type) { case BOOLEAN: assertEquals(field, 2, distinct.size()); break; case OBJECT: if (fieldName.equals("columnRealmObject")) { assertEquals(field, numberOfBlocks, distinct.size()); } else if (fieldName.equals("columnRealmLink")){ assertEquals(field, numberOfBlocks * numberOfObjects, distinct.size()); } else { fail("Unknown object " + fieldName); } break; default: assertEquals(field, numberOfObjects, distinct.size()); break; } } else { try { realm.where(AllTypes.class) .distinct(field) .findAll(); fail(); } catch (IllegalArgumentException ignore) { } } types.remove(type); } assertEquals(types.toString(), Sets.newSet(RealmFieldType.LINKING_OBJECTS), types); RealmResults<AllTypes> distinct = realm.where(AllTypes.class) .distinct(prefix + AllTypes.FIELD_REALMBACKLINK) .findAll(); assertEquals(numberOfBlocks * numberOfObjects, distinct.size()); }