Realm, RxSwift, RxRealm MVVM
yohei SUGIGAMIRealm Centered DesignRxSwift+RxRealm(RealmNotification)Aug. 23, 2016 @ Wantedlypotatotips #32Realmத৺ઃܭ
View Slide
γΰτͰίίϩΦυϧਿ্ ༸ฏYohei SUGIGAMIID susieyyTwitterˍQiitaˍGithub
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008realm notificationΛͬͯ·͔͢ʁ
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008addNotificationBlockϝιουͰϒϩοΫΛొ͢ΔͱΦϒδΣΫτ͕ߋ৽͞Εͨͱ͖௨Λड͚Δ͜ͱ͕ͰΔʢਆػೳʣ
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008Aը໘ Bը໘WriteTxNotificationAը໘ͰͷมߋΛBը໘ʹ௨Ͱ͖Δ
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008Code exampleͱ͘ʹྻσʔλͷࢹͷࠩIndex௨͕͢Β͍͠ʂ
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008RxSwiftRxRealmxmeets
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008try! Realm().objects(ContactRealm).asObservableChangeset().subscribeNext { [weak self] results, changeset inguard let tableView = self?.tableView else { return }self?.results = resultsif 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ͱͯ͠ѻ͑Δʂ
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008// Simple Create[MessageRealm("hello"), MessageRealm("world")].toObservable().subscribe(Realm.rx_add())// With APIlet request = API.Endpoint.MessagesRequest().request()Session.rx_response(request).subscribe(Realm.rx_add())RealmObjectͷCRUDRwSwiftͷཧԼʹʂ
γΰτͰίίϩΦυϧBLaunch iPhone3GiOS Developer since mid 2008RxSwift MVVMRealm Centered DesignRxRealm
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008View ViewModelAը໘Bը໘Cը໘Subscribe௨৴தͳͲը໘ݻ༗ͷঢ়ଶRxSwift w/MVVMը໘ͷओཁͳσʔλը໘ԣஅతͳσʔλͷѻ͍͕͍͠ʢLikeʣ
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008View ViewModelAը໘Bը໘Cը໘Subscribeը໘ԣஅతͳঢ়ଶɾσʔλ௨৴தͳͲը໘ݻ༗ͷঢ়ଶearth(Singleton)ٿͰ͔͍ͬγϯάϧτϯRxSwift w/MVVM(earth)ը໘ͷओཁͳσʔλGlobal ViewModel©
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008View ViewModelAը໘Bը໘Cը໘Observable StoreSubscribeը໘ԣஅͳঢ়ଶը໘ͷओཁͳσʔλ௨৴தͳͲը໘ݻ༗ͷঢ়ଶearth(Singleton)ٿͰ͔͍ͬγϯάϧτϯRxSwift+RxRealm w/MVVM(RxRealm)
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008Redux- Single source of truth- State is read-only- Changes are made with pure functionsΞϓϦͷঢ়ଶɾσʔλΛอ࣋͢ΔStoreͨͩ1ͭInspired by
γΰτͰίίϩΦυϧLaunch iPhone3GiOS Developer since mid 2008ΫϥΠΞϯτΞϓϦঢ়ଶɾσʔλཧෳࡶ- ෭࣍తͳ࢈ͱͯ͠ɺRxDatasourceʹΑΔTableViewBinding͍ʹ͍͘ͷͰRealmNotificationʹΑΔTableViewReloadѻ͍͍͢㱺 Rx w/ MVVMͱReduxͷ͍͍ͱ͜ΖͲΓΛ͍ͨ͠- ը໘ʹඥͮ͘σʔλͱঢ়ଶΛViewModelʹ٧ΊΔͷɺΞϓϦશମͷσʔλͱঢ়ଶΛStoreʹ٧ΊΔͷ྆ۃͰਏ͍- “ٿͰ͔͍ͬγϯάϧτϯ”ΤϙοΫϝΠΩϯά- ը໘ʹඥͮ͘ঢ়ଶΛ֤ViewModelʹσʔλΛStoreʢRealm or Singletonʣʹͱ͍͏ંҊΛߟ͑ͨ·ͱΊ
5IBOLZPV