Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RxSwift-MVVM
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
chocovayashi
July 24, 2018
Programming
0
130
RxSwift-MVVM
chocovayashi
July 24, 2018
Tweet
Share
More Decks by chocovayashi
See All by chocovayashi
iosの新機能軽く使ってみた
chocovayashi
0
73
Other Decks in Programming
See All in Programming
CSC307 Lecture 10
javiergs
PRO
1
660
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.4k
ノイジーネイバー問題を解決する 公平なキューイング
occhi
0
100
高速開発のためのコード整理術
sutetotanuki
1
400
CSC307 Lecture 06
javiergs
PRO
0
690
Data-Centric Kaggle
isax1015
2
780
CSC307 Lecture 07
javiergs
PRO
1
550
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
120
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
CSC307 Lecture 01
javiergs
PRO
0
690
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
710
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Featured
See All Featured
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
61
52k
Mobile First: as difficult as doing things right
swwweet
225
10k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
160
Accessibility Awareness
sabderemane
0
53
First, design no harm
axbom
PRO
2
1.1k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
730
The agentic SEO stack - context over prompts
schlessera
0
640
Docker and Python
trallard
47
3.7k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
Transcript
RxSwiftを使った iOSアプリ開発の設計 @chocovayashi
今日のゴール RxSwiftを使ってMVVMの 実装ができるようになる
目次 • RxSwiftの説明 ◦ What is RxSwift ◦ When use
RxSwift ◦ How to use RxSwift ◦ RxSwift導入よる利点 • 実際に実装してみよう ◦ Rx入れないMVVM ◦ Rx入れるMVVM • まとめ
目次 • RxSwiftの説明 ◦ What is RxSwift ◦ When use
RxSwift ◦ How to use RxSwift ◦ RxSwift導入よる利点 • 実際に実装してみよう ◦ Rx入れないMVVM ◦ Rx入れるMVVM • まとめ
What is RxSwift RxSwift = Rx + Swift Rx =
Reactive Extensions Reactive Extensionsとは・・ イベントストリームを抽象化するライブラリ
時間 【特徴】 • 値、エラー、完了の3種類のイベントが流れる。 • エラー、完了が流れると、それ以降イベントは流れない。 • イベントをlistenすることをsubscribeすると言う。 イベントが流れてくる川のようなイメージ What
is RxSwift ストリーム
When use RxSwift UIイベント 通信処理 ボタンのタップや、TextFieldの入力、 TableViewへのデータのbindなど HTTP Requestを送信する場合など
How to use RxSwift (UIButton) tapイベント
入力イベント How to use RxSwift (UITextField)
2 5 8 変更イベント How to use RxSwift (KVO)
RxSwift導入による利点 非同期処理が簡単に実現可能
可読性の向上 RxSwift使わない場合 RxSwift使わない場合 メソッドの目移りが なくなる RxSwift導入による利点
イベントの加工や結合などが簡単にできる 1 2 3 2 4 6 map { $0
* 2 } 1 2 3 6 reduce(0) { $0 + $1 } RxSwift導入による利点
1 2 3 ストリームの結合などが簡単にできる 1 2 3 4 5 6
(例) merge observable1 observable2 4 5 6 output RxSwift導入による利点
1 2 3 ストリームの結合などが簡単にできる 1 2 3 4 5 6
(例) concat observable1 observable2 4 5 6 output RxSwift導入による利点
目次 • RxSwiftの説明 ◦ What is RxSwift ◦ When use
RxSwift ◦ How to use RxSwift ◦ RxSwift導入よる利点 • 実際に実装してみよう ◦ Rx入れないMVVM ◦ Rx入れるMVVM • まとめ
今回つくるもの Github APIにアクセスして その内容をViewに表示するア プリ
MVVM作成(共通) Entityの定義 swift4から導入された、 codableを用いて、CodingKeysに jsonのkeyを書くことにより 自動で構造体にマッピング される。
MVVM作成(共通) Requestの定義
Rxを入れないMVVM ライブラリを使わないMVVM • willSet / didSetを用いるパターン • Notificationを使うパターン 動的に監視を追加、削除が可能なので 今回はNotificationを利用
Rxを入れないMVVM Notification.Nameの拡張 keyを通知送信側と受信側で直接書くのは危ういので、 Extentionを用いて Notification.Name の拡張をすべき
Modelの実装 notificationの送信 Rxを入れないMVVM
ViewModelの実装 notificationの受け取り準備 notificationの解放 Rxを入れないMVVM
KVOのデメリット • 毎回解放するのが面倒 • Notification.Nameの拡張が大量に必要となる • 可読性が低い Rxを入れないMVVM
RxSwiftとAPIKitを連携するために、Sessionを拡張 Rxを入れるMVVM
Model Rxを入れるMVVM
ViewModel 取得したEntityを変数にbind VCからsubscribeされるもの Rxを入れるMVVM
ViewController VMでbindされたentityをlistenする VMを通じてデータを取得する Rxを入れるMVVM
RxSwiftのメリット • データの流れをシンプルに書ける • 可読性が高い RxSwiftのデメリット • 学習コストが高い Rxを入れるMVVM
まとめ RxSwiftを使うとシンプルにMVVMの 非同期処理を実現できる。 けど、学習コストが高い( ´∀`) 導入するかはチームで要相談