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 @ 社内RxSwift勉強会
Search
ukitaka
July 19, 2016
Programming
5
1.2k
マルチスレッドRxSwift @ 社内RxSwift勉強会
マルチスレッドRxSwift @ 社内RxSwift勉強会
ukitaka
July 19, 2016
Tweet
Share
More Decks by ukitaka
See All by ukitaka
CSの生産性改善を支える分析環境 Mercari CS/CRE Tech Talk #1
ukitaka
1
2.6k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
410
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
5.7k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
理論から入門するswift/lib/Sema - わいわいswiftc #1
ukitaka
5
1.6k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
880
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
RxSwift コードリーディングの勘所@社内RxSwift勉強会
ukitaka
3
1k
Other Decks in Programming
See All in Programming
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
640
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
2.1k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
440
C#/.NETのこれまでのふりかえり
tomokusaba
1
170
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
9
1k
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.4k
CSC509 Lecture 09
javiergs
PRO
0
120
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
980
僕がつくった48個のWebサービス達
yusukebe
18
17k
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
380
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.3k
gopls を改造したら開発生産性が高まった
satorunooshie
8
250
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
How to Think Like a Performance Engineer
csswizardry
19
1.1k
Six Lessons from altMBA
skipperchong
26
3.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
14
2k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Code Reviewing Like a Champion
maltzj
519
39k
Transcript
ϚϧνεϨουRxSwift @ࣾRxSwiftษڧձ
߹ΘͤͯಡΈ͍ͨ https://https://speakerdeck.com/ukitaka/rxswift-kodorideingufalsekan-suo-at-she-nei-rxswiftmian-qiang-hui/bricklife/rxswiftfalseobserveontosubscribeonwoli-jie-suru
ཁίʔυϦʔσΟϯά • ޙͰৄ͘͠આ໌͠·͕͢ɺʮͲͷεϨουͰ subscribe͕ߦΘΕ͔ͨʯ͕ͱͯॏཁʹͳ͖ͬͯ ·͢ɻ • ͜͜Ͱ͍͏subscribe෦తͳsubscribeؚΈ ·͢ɻͭ·ΓʮOperator͕͍ͭͲͷεϨουͰ sourceΛsubscribe͢Δ͔ʯΛѲ͠ͳ͍ͱɺڍಈ Λཧղ͢Δͷ͕͍͜͠ͱ͋Δ͔͠Ε·ͤΜɻ
͜ͷεϥΠυͰ • ·ͣɺRxSwiftͱGCDͳͲͷΈ߹ΘͤʹΑΔ ϚϧνεϨουڥͰͷڍಈͷ֬ೝͱಈ࡞ϧʔ ϧͷઆ໌Λߦ͍·͢ɻ • ͦͷޙobserveOnɺsubscribeOnͷڍಈͷઆ ໌Λߦ͍·͢ɻ
͜ͷεϥΠυͰ • ϚϧνεϨουͱ͍ͬͯɺଟ͘ͷiOS։ൃऀ͕ڵຯ͕͋Δͷ ʮϝΠϯεϨουʯ͔ʮͦΕҎ֎͔ʯͩͱࢥ͍·͢ɻ • ͜ͷεϥΠυͰɺʮϝΠϯεϨουͰͳ͍εϨουʯΛ·ͱ ΊͯʮόοΫάϥϯυεϨουʯͱݺͿ͜ͱʹͯ͠ɺϝΠϯ εϨου͔όοΫάϥϯυεϨου͔ͷΈʹ͠·͢ɻ • ͭ·ΓόοΫάϥϯυεϨουෳ͋Γɺ࣮ࡍʹͲͷεϨο
υͰॲཧ͞ΕΔ͔ɺ(RxSwift͕෦తʹར༻͍ͯ͠Δ) GCD NSOperationQueueʹґଘ͢Δ͜ͱʹҙ͍ͯͩ͘͠͞ɻ
͜ͷεϥΠυͰ • RxSwift v2.5.0 (20167݄࣌Ͱͷmaster) Λݟͯ·͢ɻ • ͨͿΜόʔδϣϯ1ܥࠓޙͷόʔδϣϯͰ ௨༻͢Δ͔ෆ໌Ͱ͕͢ɺͨͿΜେ͖͘ζϨ Δ͜ͱແ͍ͱࢥ͍·͢ɻ
લฤ Observableͱ εϨου
ಈ࡞ϧʔϧ
ϧʔϧͦͷ1 ௨ৗͷColdͳOperator subscribe͞ΕͨεϨουͰsubscribe on͞ΕͨεϨουͰonΛݺͿɻ
ϧʔϧͦͷ1(ଓ͖) ಛʹSchedulerͷࢦఆͳͲ͕ͳ͍߹ɺ createܥOperatorsubscribeͨ͠ε ϨουͰonΛݺͿɻ
ݴ༿͚ͩͩͱΑ͘Θ͔ΒΜ
None
Sequence Observer
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου on
Question1 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
ղઆ subscribe Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου ݺΕͨεϨουͰ෦తʹsourceͷ subscribeΛߦ͏ɻ
ղઆ subscribe Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe͕ݺΕͨεϨουonΛݺͿ
ղઆ subscribe Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥϯυεϨου on
Question2 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ Sequence Map Observer Publish
ղઆ Sequence Map Observer Publish subscribe
ղઆ Sequence Map Observer subscribe Publish subscribe
ղઆ subscribe Sequence Map Observer subscribe Publish subscribe
ղઆ subscribe Sequence Map Observer subscribe Publish subscribe subscribe͕ݺΕͨεϨουonΛݺͿ
ղઆ subscribe Sequence Map Observer subscribe on on Publish subscribe
on
(ิ) ղઆ subscribe Sequence Map Observer subscribe on on Publish
subscribe on ಛʹઆ໌ͯ͠ͳ͔͕ͬͨɺSubject on͞ΕͨεϨουͰonΛݺͿ
Question3 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ • refCount() ʮHot͕ͩɺ࠷ॳʹsubscribe͞ ΕΔ·Ͱಈ࡞͠ͳ͍ʯͱ͍͏ڍಈɻͭ·Γ࠷ ॳʹsubscribe͕ݺΕͨεϨου͕ॏཁɻ • ࠓճ࠷ॳʹsubscribeΛݺΜͰ͍Δͷ͕όο ΫάϥϯυεϨουͳͷͰόοΫάϥϯ υͰಈ࡞͢Δɻ
(࠶ܝ) ϧʔϧͦͷ1 ௨ৗͷColdͳOperator subscribe͞ΕͨεϨουͰsubscribe on͞ΕͨεϨουͰonΛݺͿɻ
(࠶ܝ) ϧʔϧͦͷ1(ଓ͖) ಛʹSchedulerͷࢦఆͳͲ͕ͳ͍߹ɺ createܥOperatorsubscribeͨ͠ε ϨουͰonΛݺͿɻ
࣍ͷϧʔϧ…
ϧʔϧͦͷ2 createܥͷOperatorʹScheduler͕ࢦ ఆ͞Ε͍ͯΔ߹ɺsubscribe͢Δͱͦ ͷScheduler্ͰonΛݺͿɻ ͭ·ΓɺͲͷεϨουͰsubscribe͞Ε ͔ͨؔͳ͍ɻ (͍)
Scheduler • createܥͷOperatorʹɺSchedulerΛࢦఆͰ ͖Δͷ͕͋Δɻ
Scheduler • Scheduler͕ࢦఆ͞Ε͍ͯΔ߹ʹͲ͏͍͏ڍ ಈʹͳΔ͔ɺͱ͍͏ͷ͕ࠓճͷϧʔϧ
None
Scheduler͕ࢦఆ͞Ε͍ͯΔ
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου on
ͭ·Γ • Scheduler͕ࢦఆ͞Ε͍ͯΔ߹Ͳ͜Ͱ subscribeΛݺ΅͏͕ɺࢦఆ͞Εͨ Scheduler(εϨου)ͰonΛݺͿɻ
Question4 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
֤.Next, .CompletedͲͷεϨουͰon͞ΕΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. حόοΫάϥϯυεϨου ۮϝΠϯεϨου .Completedෆఆ
ղઆ • .Nextʹ͍ͭͯ͑؆୯Ͱ͕͢ɺ୭͕Ͳ͏ಈ ͍͍ͯΔͷ͔ΠϝʔδͰ͖ΔͱΑΓྑ͍ͱࢥ͍ ·͢ɻ • ͭ·ΓMergeSinkIter͕subscribeΛ͢Δ → Scheduler͕ࢦఆ͞Ε͍ͯΔͷͰͦͷεϨουͰ on͕ݺΕͯobserverʹ͢ͱ͍͏͜ͱͰ͢ɻ
ղઆ • .CompletedෆఆͰ͢ɻෳͷqueueʹ dispatch͍ͯ͠ΔͷͰɺ࠷ޙʹྲྀΕ͖ͯ ͨ.Next͕ͲͷεϨου͔ͩͬͨͰ.Completed ͕ྲྀΕͯ͘ΔεϨου͕มΘΓ·͢ɻ • ·͊ී௨͜Μͳ͍ํ͠ͳ͍ͱࢥ͍·͢ ͕…
Question5 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
Replay͞ΕΔ.Next 1. .NextΛ࠷ॳʹܭࢉͨ͠εϨου 2. subscribeͨ͠εϨου ͲͪΒͰྲྀΕͯ͘ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. subscribeͨ͠εϨου
ղઆ • replaysubscribe͞ΕͨεϨουͰߦΘΕ· ͢ɻ • ·͊ͦΕ͚ͩͰ͢ɻɻɻ
(࠶ܝ) ϧʔϧͦͷ2 createܥͷOperatorʹScheduler͕ࢦ ఆ͞Ε͍ͯΔ߹ɺsubscribe͢Δͱͦ ͷScheduler্ͰonΛݺͿɻ ͭ·ΓɺͲͷεϨουͰsubscribe͞Ε ͔ͨؔͳ͍ɻ (͍)
ObservableͱεϨουʹؔ͢Δϧʔϧ 1. ௨ৗͷColdͳOperatorsubscribe͞ΕͨεϨουͰ subscribeɺon͞ΕͨεϨουͰonΛݺͿɻಛʹ SchedulerͷࢦఆͳͲ͕ͳ͍߹ɺcreateܥOperator subscribeͨ͠εϨουͰonΛݺͿ 2. createܥͷOperatorʹScheduler͕ࢦఆ͞Ε͍ͯΔ ߹ɺsubscribe͢ΔͱͦͷScheduler্ͰonΛݺͿɻͭ ·ΓɺͲͷεϨουͰsubscribe͞Ε͔ͨؔͳ͍ɻ
ޙฤ observeOn, subscribeOn
observeOn, subscribeOn • ͔͜͜ΒScheduler(εϨου)Λมߋ͢Δͨ ΊͷOperator observeOn, subscribeOnʹͭ ͍ͯݟ͍͖ͯ·͕͢ɺ͜͜·Ͱͷ͕ͳΜͱ ͳ͘ཧղͰ͖͍ͯΕಛʹ͘͠ͳ͍ͱࢥ͍ ·͢ɻ
observeOn on͢ΔScheduler(εϨου)Λ มߋ͢Δ
subscribeOn subscribe͢ΔScheduler(εϨου)Λ มߋ͢Δ
ͦͷ··ʂʂ
ղઆ Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥϯυεϨου observeOn .observeOn(backgroundScheduler)
subscribe subscribe on on
ղઆ Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥϯυεϨου observeOn .observeOn(backgroundScheduler)
subscribe subscribe on on
ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribeOn .subscribeOn(backgroundScheduler) subscribe
on on on subscribe
ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribeOn .subscribeOn(backgroundScheduler) subscribe
on on on subscribe
observeOn • observeOnʹ͍ͭͯɺͦΕΑΓલͷจ຺ʹ ؔͳ͑͘ΔͷͰɺಛʹ͍͜͠ͱΛߟ͑ ͣʹʮΠϕϯτ͕ྲྀΕͯ͘ΔεϨουΛม͑ ͍ͨʂʯͱ͍͏ͱ͖ʹ͑Δͱࢥ͍·͢ɻ
subscribeOn • subscribeOnʹ͍͔ͭͯͳΓ෦ͷಈ͖ʹ ґଘ͠·͕͢ɺཁલฤͰΈͨ௨ΓͰɺͬ͟ ͘Γݴͬͯ͠·͑ҎԼಉ͡Ͱ͢ɻ
subscribeOn • લͷจ຺ʹ͔ͳΓґଘ͢ΔͷͰɺͦͦ subscribeOn͕ޮ͔ͳ͍͜ͱ͢Β͋Γ·͢ɻ • ͍υίϩͱͯ͠ɺcreateܥͷOperatorͷ ޙʹॻ͍͓͍ͯͯͦͷScheduler(εϨου) Ͱ͔͠ಈ࡞͠ͳ͍ObservableΛ࡞Δ͜ͱ͕Ͱ ͖·͢ɻ
Question6 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
ͦΕͧΕͷMapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe on on on subscribe Sequence
Map Observer subscribeOn Map subscribe on
Question7 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
None
ͦΕͧΕͷMapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. ϝΠϯεϨου
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn
Map subscribe
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn
Map subscribe SequenceMainSchedulerͰಈ࡞͢Δɻ
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn
Map subscribe on on on on
Question8 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
ͦΕͧΕͷMapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ • (ུ)
(࠶ܝ) observeOn on͢ΔScheduler(εϨου)Λ มߋ͢Δ
(࠶ܝ) subscribeOn subscribe͢ΔScheduler(εϨου)Λ มߋ͢Δ
͓·͚ Driver
Driver • DriverRxCocoa͕ఏڙ͍ͯ͠ΔɺUIͱͭͳ ͛Δͱ͖ʹྑ͍ײ͡ʹͬͯ͘ΕΔศརͳ Observableతͳ͛͢ʔͭɻ(ޠኮྗ) • ࡉ͔͍ੑ࣭͍Ζ͍Ζ͋ΔΜͰ͕͢ɺεϨο υपΓͷੑ࣭ʹ͍ͭͯݟͯΈ·͢ɻ
Driverͷ࡞Γํ • createܥͷOperatorΛར༻͢Δ • Observable͔Βม͢Δ
createܥͷOperatorΛར༻͢Δ • Driverʹಉ͡Α͏ʹcreate, just, ofͳͲͷ Operator͕ଘࡏ͢Δɻ • ίʔυΛಡΉͱΈ؆୯ͰɺϝΠϯεϨο υͰsubscribe͕͞ΕΔΑ͏ʹsubscribeOnΛ ݺΜͰ͍Δ͜ͱ͕Θ͔Δɻ
createܥͷOperatorΛར༻͢Δ
Observable͔Βม͢Δ • asDriverͰObservableΛDriverʹͰ͖Δ • ͬͪ͜؆୯ͰɺobserveOnͰϝΠϯεϨο υͰಈ࡞͢ΔΑ͏ʹ͍ͯ͠Δɻ
Observable͔Βม͢Δ
drive, driveNext • Driver ʹ subscribe ͕ͳ͘ɺ୯७ʹΘΓ ʹdrive driveNext ͳͲΛར༻͢Δɻ
• 1͚ͭͩҙ͕͋ͬͯɺ͜ΕΒϝΠϯε ϨουͰ͔͠ݺͳ͍ɻ
drive, driveNext
DriverͷOperatorʹ͍ͭͯ • mapͳͲɺ௨ৗͷOperator͍͍ͩͨҰ௨Γ ༻ҙ͞Ε͍ͯΔɻ • ͜ΕΒΛݺΜͰಛʹεϨουʹӨڹͳ͍ɻ • driveOnSchedulerͱ͍͏Operator͕͋Δ͕ɺ ͜Εςετ༻ͳͷͰΘͳ͍Α͏ʹɻ
Question9 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. Τϥʔ
ղઆ • driveϝΠϯεϨου͔Β͔͠ݺ·ͤΜɻ (ͻ͔͚ͬʣ
Question10 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. ϝΠϯεϨου
ղઆ ϝΠϯεϨου όοΫάϥϯυεϨου subscribe Sequence Map Observer drive on on
Question11 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ ObserverʹͲͷεϨουͰΠϕϯτ͕ಧ͘Ͱ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. mapόοΫάϥϯυ observerϝΠϯ
ղઆ ϝΠϯεϨου όοΫάϥϯυεϨου subscribe Sequence Map Observer drive on on
͓·͚ ͦͷ2 NSURLSession+Rx
NSURLSession+Rx • rx_responserx_JSONόοΫάϥϯυε ϨουͰonΛݺΜͰ͘ΔͷͰɺωοτϫʔΫ → UI ΛΔͱ͖ؾΛ͚ͭΑ͏ɻ
·ͱΊ
ObservableͱεϨουʹؔ͢Δϧʔϧ 1. ௨ৗͷColdͳOperatorsubscribe͞ΕͨεϨουͰ subscribeɺon͞ΕͨεϨουͰonΛݺͿɻಛʹ SchedulerͷࢦఆͳͲ͕ͳ͍߹ɺcreateܥOperator subscribeͨ͠εϨουͰonΛݺͿ 2. createܥͷOperatorʹScheduler͕ࢦఆ͞Ε͍ͯΔ ߹ɺsubscribe͢ΔͱͦͷScheduler্ͰonΛݺͿɻͭ ·ΓɺͲͷεϨουͰsubscribe͞Ε͔ͨؔͳ͍ɻ
observeOn, subscribeOn 1. subscribeOnsubscribe͢ΔScheduler(ε Ϩου)Λมߋ͢Δɻ 2. observeOnon͢ΔScheduler(εϨου)Λ มߋ͢Δɻ
Ҏ্ʂʂ