Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Realm World Tour Tokyo Recent Java Updates

zaki50
March 19, 2017

Realm World Tour Tokyo Recent Java Updates

zaki50

March 19, 2017
Tweet

More Decks by zaki50

Other Decks in Technology

Transcript

  1. Java Updates Makoto Yamazaki #RealmWorld

  2. Realm Java 3.0.0 [email protected]

  3. Realm Java 2.3.0(৽ػೳ) • จࣈྻʹର͢Δlikeݕࡧ [email protected] query.like("text", "*");
 query.like("text", "?ohn*");

    ?: ೚ҙͷϢχίʔυจࣈ1จࣈ *: 0จࣈҎ্ͷϢχίʔυจࣈ
  4. Realm Java 3.0.0(৽ػೳ) • ৄࡉͳมߋ௨஌(Fine-grained Collection Notification) • RealmListͷมߋ௨஌αϙʔτ •

    RealmListͷObservableαϙʔτ • RealmResultsͷଈ࣌ߋ৽ͷ෮׆ͱɺεφοϓγϣοτػೳ • ϦϯΫϑΟʔϧυͰͷιʔτ • RealmObjectͷϦεφʔ͕࡟আ࣌ʹ΋ݺ͹ΕΔΑ͏ʹͳͬͨ [email protected]
  5. Realm Java 3.0.0(ඇޓ׵ͳมߋ) • RealmResults#distinct()͕৽ͨͳRealmResultsΛฦ͢ • RealmResultsͷଈ࣌ߋ৽ • RealmObjectͷϦεφʔ͕࡟আ࣌ʹ΋ݺ͹ΕΔΑ͏ʹͳͬͨ [email protected]

  6. ैདྷͷίϨΫγϣϯ௨஌ [email protected] public interface RealmChangeListener<T> {
 /**
 * Called when

    a transaction is committed.
 */
 void onChange(T element);
 }
  7. ैདྷͷίϨΫγϣϯ௨஌ [email protected] RealmChangeListener<RealmResults<Dog>> listener; final RealmResults<Dog> puppies = realm.where(Dog.class)
 .lessThan("age",

    2).findAll();
 listener = new RealmChangeListener<RealmResults<Dog>>() {
 @Override
 public void onChange(RealmResults<Dog> collection) {
 listView.notifyDatasetChanged();
 }
 };
 puppies.addChangeListener(listener);
  8. ৄࡉͳίϨΫγϣϯ௨஌ [email protected] public interface OrderedRealmCollectionChangeListener<T> {
 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;
 } }
  9. ৄࡉͳίϨΫγϣϯ௨஌ [email protected] OrderedRealmCollectionChangeListener<RealmResults<Dog>> listener; final RealmResults<Dog> puppies = realm.where(Dog.class)
 .lessThan("age",

    2).findAll();
 listener = new OrderedRealmCollectionChangeListener<RealmResults<Dog>>() {
 @Override
 public void onChange(RealmResults<Dog> collection, OrderedCollectionChangeSet changeSet) {
 ...
 }
 };
 puppies.addChangeListener(listener);
  10. ৄࡉͳίϨΫγϣϯ௨஌ [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);
 }
 }
  11. General header [email protected] RecyclerViewͷdemo

  12. 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<Dog, ViewHolder> {
 ...
 }

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

  14. RealmResultsͱεφοϓγϣοτ [email protected] OrderedRealmCollection<Guest> uninvited = realm.where(Guest.class)
 .equalTo("invited", false)
 .findAll();
 


    for (i = 0; i < uninvited.size(); i++) {
 realm.beginTransaction(); uninvitedGuests.get(i).invited = true;
 realm.commitTransaction();
 }

  15. RealmResultsͱεφοϓγϣοτ [email protected] OrderedRealmCollection<Guest> uninvited = realm.where(Guest.class)
 .equalTo("invited", false)
 .findAll().createSnapshot();
 


    for (i = 0; i < uninvited.size(); i++) {
 realm.beginTransaction();
 uninvitedGuests.get(i).invited = true;
 realm.commitTransaction();
 }

  16. ϦϯΫϑΟʔϧυͰͷιʔτ [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<Person> persons = realm.where(Person.class) .findAllSorted("dog.age", Sort.ASCENDING);
  17. Realm Java 3.1ͷ༧ఆ • LinkingObjectαϙʔτ • RealmObjectͷৄࡉͳมߋ௨஌ • ಉظঢ়گͷ௨஌ػೳ [email protected]

  18. 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<Person> owners;
 } RealmResults<Person> owners; owners = realm.where(Dog.class).findAll().first().owners; owners.size();