Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
420
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
6.4k
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
距離関数を極める! / SESSIONS 2024
gam0022
0
340
macOS なしで iOS アプリを開発する(※ただし xxx に限る)
mitsuharu
1
150
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.9k
React への依存を最小にするフロントエンド設計
takonda
21
8.3k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
340
Vapor Revolution
kazupon
2
2.4k
Java 23の概要とJava Web Frameworkの現状 / Java 23 and Java web framework
kishida
2
360
as(型アサーション)を書く前にできること
marokanatani
10
2.9k
「天気予報があなたに届けられるまで」 - NIFTY Tech Talk #22
niftycorp
PRO
0
130
flutterkaigi_2024.pdf
kyoheig3
0
350
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
130
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
260
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
17k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
We Have a Design System, Now What?
morganepeng
50
7.2k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Speed Design
sergeychernyshev
25
640
How to Think Like a Performance Engineer
csswizardry
20
1.1k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
[RailsConf 2023] Rails as a piece of cake
palkan
52
5k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
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(εϨου)Λ มߋ͢Δɻ
Ҏ্ʂʂ