Slide 1

Slide 1 text

Java Updates Makoto Yamazaki #RealmWorldTour17

Slide 2

Slide 2 text

Realm Java 3.0.0 [email protected]

Slide 3

Slide 3 text

Realm Java 2.3.0(৽ػೳ) • จࣈྻʹର͢Δlikeݕࡧ [email protected] query.like("text", "*");
 query.like("text", "?ohn*"); ?: ೚ҙͷϢχίʔυจࣈ1จࣈ *: 0จࣈҎ্ͷϢχίʔυจࣈ

Slide 4

Slide 4 text

Realm Java 3.0.0(৽ػೳ) • ৄࡉͳมߋ௨஌(Fine-grained Collection Notification) • RealmListͷมߋ௨஌αϙʔτ • RealmListͷObservableαϙʔτ • RealmResultsͷଈ࣌ߋ৽ͷ෮׆ͱɺεφοϓγϣοτػೳ • ϦϯΫϑΟʔϧυͰͷιʔτ • RealmObjectͷϦεφʔ͕࡟আ࣌ʹ΋ݺ͹ΕΔΑ͏ʹͳͬͨ [email protected]

Slide 5

Slide 5 text

Realm Java 3.0.0(ඇޓ׵ͳมߋ) • RealmResults#distinct()͕৽ͨͳRealmResultsΛฦ͢ • RealmResultsͷଈ࣌ߋ৽ • RealmObjectͷϦεφʔ͕࡟আ࣌ʹ΋ݺ͹ΕΔΑ͏ʹͳͬͨ [email protected]

Slide 6

Slide 6 text

ैདྷͷίϨΫγϣϯ௨஌ [email protected] public interface RealmChangeListener {
 /**
 * Called when a transaction is committed.
 */
 void onChange(T element);
 }

Slide 7

Slide 7 text

ैདྷͷίϨΫγϣϯ௨஌ [email protected] RealmChangeListener> listener; final RealmResults puppies = realm.where(Dog.class)
 .lessThan("age", 2).findAll();
 listener = new RealmChangeListener>() {
 @Override
 public void onChange(RealmResults collection) {
 listView.notifyDatasetChanged();
 }
 };
 puppies.addChangeListener(listener);

Slide 8

Slide 8 text

ৄࡉͳίϨΫγϣϯ௨஌ [email protected] public interface OrderedRealmCollectionChangeListener {
 void onChange(T collection, OrderedCollectionChangeSet changeSet); } public interface OrderedCollectionChangeSet {
 int[] getDeletions();
 int[] getInsertions();
 int[] getChanges();
 
 Range[] getDeletionRanges();
 Range[] getInsertionRanges();
 Range[] getChangeRanges();
 
 class Range {
 public final int startIndex;
 public final int length;
 } }

Slide 9

Slide 9 text

ৄࡉͳίϨΫγϣϯ௨஌ [email protected] OrderedRealmCollectionChangeListener> listener; final RealmResults puppies = realm.where(Dog.class)
 .lessThan("age", 2).findAll();
 listener = new OrderedRealmCollectionChangeListener>() {
 @Override
 public void onChange(RealmResults collection, OrderedCollectionChangeSet changeSet) {
 ...
 }
 };
 puppies.addChangeListener(listener);

Slide 10

Slide 10 text

ৄࡉͳίϨΫγϣϯ௨஌ [email protected] public void onChange(Object collection, OrderedCollectionChangeSet changeSet) {
 if (changeSet == null) {
 notifyDataSetChanged();
 return;
 }
 OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges();
 for (int i = deletions.length - 1; i >= 0; i--) {
 OrderedCollectionChangeSet.Range range = deletions[i];
 notifyItemRangeRemoved(range.startIndex, range.length);
 }
 
 OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges();
 for (OrderedCollectionChangeSet.Range range : insertions) {
 notifyItemRangeInserted(range.startIndex, range.length);
 }
 
 OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges();
 for (OrderedCollectionChangeSet.Range range : modifications) {
 notifyItemRangeChanged(range.startIndex, range.length);
 }
 }

Slide 11

Slide 11 text

[email protected] RecyclerViewͷdemo

Slide 12

Slide 12 text

Realm Android Adapters [email protected] https://github.com/realm/realm-android-adapters/ dependencies { compile 'io.realm:android-adapters:2.0.0' } class MyRecyclerViewAdapter extends RealmRecyclerViewAdapter {
 ...
 }


Slide 13

Slide 13 text

εφοϓγϣοτ [email protected] • RealmResultsʹଈ࠲ʹมߋ͕൓ө͞ΕΔΑ͏ʹͳͬͨ • OrderedRealmCollectionSnapshot RealmResults#createSnapshot()Λ௥Ճ • OrderedRealmCollectionSnapshotࣗମʹ͸มߋ͕൓ө͞Εͳ͍

Slide 14

Slide 14 text

RealmResultsͱεφοϓγϣοτ [email protected] OrderedRealmCollection uninvited = realm.where(Guest.class)
 .equalTo("invited", false)
 .findAll();
 
 for (i = 0; i < uninvited.size(); i++) {
 realm.beginTransaction(); uninvited.get(i).invited = true;
 realm.commitTransaction();
 }


Slide 15

Slide 15 text

RealmResultsͱεφοϓγϣοτ [email protected] OrderedRealmCollection uninvited = realm.where(Guest.class)
 .equalTo("invited", false)
 .findAll().createSnapshot();
 
 for (i = 0; i < uninvited.size(); i++) {
 realm.beginTransaction();
 uninvited.get(i).invited = true;
 realm.commitTransaction();
 }


Slide 16

Slide 16 text

ϦϯΫϑΟʔϧυͰͷιʔτ [email protected] public class Person extends RealmObject{
 public String name;
 public Dog dog;
 }
 public class Dog extends RealmObject{
 public String name;
 public int age;
 }
 RealmResults persons = realm.where(Person.class) .findAllSorted("dog.age", Sort.ASCENDING);

Slide 17

Slide 17 text

Realm Java 3.1ͷ༧ఆ • LinkingObjectαϙʔτ • RealmObjectͷৄࡉͳมߋ௨஌ • ಉظঢ়گͷ௨஌ػೳ [email protected]

Slide 18

Slide 18 text

LinkingObjects [email protected] public class Person extends RealmObject{
 public String name;
 public Dog dog;
 } 
 public class Dog extends RealmObject{
 public String name;
 public int age; @LinkingObjects("dog") public RealmResults owners;
 } RealmResults owners; owners = realm.where(Dog.class).findAll().first().owners; owners.size();