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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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 08
javiergs
PRO
0
670
CSC307 Lecture 09
javiergs
PRO
1
840
Oxlint JS plugins
kazupon
1
980
AI時代の認知負荷との向き合い方
optfit
0
160
CSC307 Lecture 07
javiergs
PRO
1
550
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.6k
Grafana:建立系統全知視角的捷徑
blueswen
0
330
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
CSC307 Lecture 02
javiergs
PRO
1
780
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
610
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
460
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
21
7.3k
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The Curse of the Amulet
leimatthew05
1
8.7k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
220
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.2k
Paper Plane
katiecoart
PRO
0
46k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
450
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
120
Tell your own story through comics
letsgokoyo
1
810
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
240
Mobile First: as difficult as doing things right
swwweet
225
10k
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の 非同期処理を実現できる。 けど、学習コストが高い( ´∀`) 導入するかはチームで要相談