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

Realm Centered Design

Realm Centered Design

Realm, RxSwift, RxRealm MVVM

yohei sugigami

August 23, 2016
Tweet

More Decks by yohei sugigami

Other Decks in Technology

Transcript

  1. yohei SUGIGAMI
    Realm Centered Design
    RxSwift+RxRealm(RealmNotification)
    Aug. 23, 2016 @ Wantedly
    potatotips #32
    Realmத৺ઃܭ

    View Slide

  2. γΰτͰίίϩΦυϧ
    ਿ্ ༸ฏ
    Yohei SUGIGAMI
    ID susieyy
    TwitterˍQiitaˍGithub

    View Slide

  3. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    realm notificationΛ஌ͬͯ·͔͢ʁ

    View Slide

  4. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    addNotificationBlockϝιουͰϒϩοΫΛ
    ొ࿥͢ΔͱΦϒδΣΫτ͕ߋ৽͞Εͨͱ͖
    ௨஌Λड͚Δ͜ͱ͕ͰΔ
    ʢਆػೳʣ

    View Slide

  5. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    Aը໘ Bը໘
    WriteTx
    Notification
    Aը໘ͰͷมߋΛBը໘ʹ௨஌Ͱ͖Δ

    View Slide

  6. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    Code example
    ͱ͘ʹ഑ྻσʔλͷ؂ࢹͷ
    ࠩ෼Index௨஌͕͢͹Β͍͠ʂ

    View Slide

  7. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    RxSwift
    RxRealm
    x
    meets

    View Slide

  8. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    try! Realm().objects(ContactRealm).asObservableChangeset()
    .subscribeNext { [weak self] results, changeset in
    guard let tableView = self?.tableView else { return }
    self?.results = results
    if let changeset = changeset {
    tableView.beginUpdates()
    tableView.insertRowsAtIndexPaths(…, withRowAnimation: .Automatic)
    tableView.deleteRowsAtIndexPaths(…, withRowAnimation: .Automatic)
    tableView.reloadRowsAtIndexPaths(…, withRowAnimation: .Automatic)
    tableView.endUpdates()
    } else {
    tableView.reloadData()
    }
    }.addDisposableTo(disposeBag)
    NotificationΛRxSwiftͷObservableͱͯ͠ѻ͑Δʂ

    View Slide

  9. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    // Simple Create
    [MessageRealm("hello"), MessageRealm("world")]
    .toObservable()
    .subscribe(Realm.rx_add())
    // With API
    let request = API.Endpoint.MessagesRequest().request()
    Session.rx_response(request)
    .subscribe(Realm.rx_add())
    RealmObjectͷCRUD΋RwSwiftͷ؅ཧԼʹʂ

    View Slide

  10. γΰτͰίίϩΦυϧ
    B
    Launch iPhone3G
    iOS Developer since mid 2008
    RxSwift MVVM
    Realm Centered Design
    RxRealm

    View Slide

  11. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    View ViewModel
    Aը໘
    Bը໘
    Cը໘
    Subscribe
    ௨৴தͳͲը໘ݻ༗ͷঢ়ଶ
    RxSwift w/
    MVVM
    ը໘ͷओཁͳσʔλ
    ը໘ԣஅతͳσʔλͷѻ͍͕೉͍͠
    ʢLike໰୊ʣ

    View Slide

  12. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    View ViewModel
    Aը໘
    Bը໘
    Cը໘
    Subscribe
    ը໘ԣஅతͳঢ়ଶɾσʔλ
    ௨৴தͳͲը໘ݻ༗ͷঢ়ଶ
    earth
    (Singleton)
    ஍ٿ͸Ͱ͔͍ͬγϯάϧτϯ
    RxSwift w/
    MVVM(earth)
    ը໘ͷओཁͳσʔλ
    Global ViewModel
    ©

    View Slide

  13. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    View ViewModel
    Aը໘
    Bը໘
    Cը໘
    Observable Store
    Subscribe
    ը໘ԣஅͳঢ়ଶ
    ը໘ͷओཁͳσʔλ
    ௨৴தͳͲը໘ݻ༗ͷঢ়ଶ
    earth
    (Singleton)
    ஍ٿ͸Ͱ͔͍ͬγϯάϧτϯ
    RxSwift+RxRealm w/
    MVVM
    (RxRealm)

    View Slide

  14. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    Redux
    - Single source of truth
    - State is read-only
    - Changes are made with pure functions
    ΞϓϦͷঢ়ଶɾσʔλΛอ࣋͢ΔStore͸ͨͩ1ͭ
    Inspired by

    View Slide

  15. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008
    ΫϥΠΞϯτΞϓϦ͸ঢ়ଶɾσʔλ؅ཧ͸ෳࡶ
    - ෭࣍తͳ࢈෺ͱͯ͠ɺRxDatasourceʹΑΔTableViewBinding͸࢖͍ʹ
    ͍͘ͷͰRealmNotificationʹΑΔTableViewReload͸ѻ͍΍͍͢
    㱺 Rx w/ MVVMͱReduxͷ͍͍ͱ͜ΖͲΓΛ͍ͨ͠
    - ը໘ʹඥͮ͘σʔλͱঢ়ଶΛViewModelʹ٧ΊΔͷ΋ɺΞϓϦશମͷσʔλͱ
    ঢ়ଶΛStoreʹ٧ΊΔͷ΋྆ۃ୺Ͱਏ͍
    - “஍ٿ͸Ͱ͔͍ͬγϯάϧτϯ”͸ΤϙοΫϝΠΩϯά
    - ը໘ʹඥͮ͘ঢ়ଶΛ֤ViewModelʹσʔλΛStoreʢRealm or Singletonʣ
    ʹͱ͍͏ં஭ҊΛߟ͑ͨ
    ·ͱΊ

    View Slide

  16. 5IBOLZPV

    View Slide