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

オフラインファーストなアプリケーション開発

zaki50
March 09, 2017

 オフラインファーストなアプリケーション開発

DroidKaigi 2017 Day1
16:00-16:50 Room 3

zaki50

March 09, 2017
Tweet

More Decks by zaki50

Other Decks in Technology

Transcript

  1. [email protected] ϞόΠϧ Billions of Minutes Online (USA) 0 250 500

    750 1000 1250 1500 Jun 2013 Jun 2014 Jun 2015 Desktop Mobile App Mobile Browser
  2. [email protected] Realm Mobile Platform ෳ਺σόΠεͷσʔλϕʔεΛϦΞϧλΠϜಉظ ΦϑϥΠϯͰ΋ϩʔΧϧͷσʔλϕʔε͕࢖͑Δ ΦϑϥΠϯ࣌ͷมߋিಥΛࣗಈղܾ SD SD SD

    SDK SD Object Store Sync
 Server RMP Dashboar d Event Auth System Encryption Layer Rules Engine Data Connect ors Real-time Sync
  3. Ϟσϧͷఆٛ [email protected] public class Task extends RealmObject { @PrimaryKey public

    String id = UUID.randomUUID().toString(); @Required
 public Date createdAt = new Date();
 
 public String text;
 public boolean completed; }
  4. σʔλͷૠೖ [email protected] final Task task = new Task();
 task.text =

    "原稿を書く"; realm.executeTransactionAsync(new Realm.Transaction() {
 @Override
 public void execute(Realm realm) {
 realm.insert(task);
 }
 });
  5. σʔλͷߋ৽ [email protected] RealmResults<Task> allTasks = realm.where(Task.class) .findAllSorted("createdAt"); // たとえばリストで position

    == 5 の要素がタップされた時 final Task task = allTasks.get(5); 
 realm.executeTransaction(new Realm.Transaction() {
 @Override
 public void execute(Realm realm) { task.completed = true; }
 });
  6. σʔλͷ࡟আ [email protected] RealmResults<Task> allTasks = realm.where(Task.class) .findAllSorted("createdAt"); // たとえばリストで position

    == 5 の要素がスワイプされた時 final Task task = allTasks.get(5); 
 realm.executeTransaction(new Realm.Transaction() {
 @Override
 public void execute(Realm realm) { task.deleteFromRealm(); }
 });
  7. มߋ௨஌ [email protected] completedTasks.addChangeListener(
 new OrderedRealmCollectionChangeListener<RealmResults<Task>>() {
 @Override
 public void onChange(RealmResults<Task>

    tasks,
 OrderedCollectionChangeSet changeSet) {
 // changeSetから、追加/削除/更新があった要素を取得 可能
 }
 });
  8. ηοτΞοϓ [email protected] // in build.gradle buildscript { repositories { jcenter()

    } dependencies { classpath "io.realm:realm-gradle-plugin:3.0.0" } } // in app/build.gradle apply plugin: 'realm-android'
  9. σʔλͷߋ৽ [email protected] RealmResults<Task> allTasks = realm.where(Task.class) .findAllSorted("createdAt"); // たとえばリストで position

    == 5 の要素がタップされた時 final Task task = allTasks.get(5); 
 realm.executeTransaction(new Realm.Transaction() {
 @Override
 public void execute(Realm realm) { task.completed = true; }
 });
  10. มߋ௨஌ [email protected] completedTasks.addChangeListener(
 new OrderedRealmCollectionChangeListener<RealmResults<Task>>() {
 @Override
 public void onChange(RealmResults<Task>

    tasks,
 OrderedCollectionChangeSet changeSet) {
 // changeSetから、追加/削除/更新があった要素を取得可能
 }
 });
  11. ϩάΠϯ [email protected] // 認証 SyncCredentials credential = SyncCredentials.usernamePassword(email, password, false);


    SyncUser user = SyncUser.login(credential, RealmTasksApplication.AUTH_URL);
 SyncConfiguration defaultConfig = new SyncConfiguration.Builder(user, RealmTasksApplication.REALM_URL) .build();
 Realm.setDefaultConfiguration(defaultConfig);