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

Realmという衝撃

 Realmという衝撃

テックランチ

Takumi KASHIMA

August 27, 2015
Tweet

More Decks by Takumi KASHIMA

Other Decks in Technology

Transcript

  1. Model public class Dog extends RealmObject { private String name;

    public String getName() { return name; } public void setName(String name) { this.name = name; } }
  2. Thread new Thread(new Runnable() { public void run() { Realm

    realm = Realm.getInstance(context); RealmResults<Dog> dogs = realm.where(Dog.class) .contains("name", "rex") .findAll(); } }).start();
  3. Transaction Realm realm = Realm.getInstance(getContext()); Dog dog = realm.createObject(Dog.class); try

    { dog.setName("Rex"); realm.commitTransaction(); } catch (Exception e) { realm.cancelTransaction(); }
  4. public class Migration implements RealmMigration { @Override public long execute(Realm

    realm, long version) { if (version == 0) { realm.getTable(Dog.class) .addColumn(ColumnType.INTEGER, "age"); version = 1; } return version; } } ࠷৽ʹͳΔΑ͏ͭͣͭίʔυʹॻ͍͍ͯ͘ ඪ४ͷϚΠάϨʔγϣϯ
  5. public class Migration implements RealmMigration { @Override public long execute(Realm

    realm, long version) { if (version == 0) { realm.getTable(Dog.class) .addColumn(ColumnType.INTEGER, "age"); version = 1; } return version; } } ࠷৽ʹͳΔΑ͏ͭͣͭίʔυʹॻ͍͍ͯ͘ ඪ४ͷϚΠάϨʔγϣϯ ϑΝΠϧʹॻ͚Δ
  6. bundle exec monarch_migration generate addColumnDog age:int { "up": [ "addcolumn

    Dog age:int" ] } } 1436076736_add_column_dog.json ϚΠάϨʔγϣϯϑΝΠϧͷ࡞੒
  7. public class Migration implements RealmMigration { @Override public long execute(Realm

    realm, long version) { HashMap<String, Class> classMap = new HashMap<String, Class>() { { put("Dog", Dog.class); } }; return Monarch.migration(getContext(), realm, version, schemeVersion, classMap); } } ϚΠάϨʔγϣϯ