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.7k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
420
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
6.6k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
理論から入門するswift/lib/Sema - わいわいswiftc #1
ukitaka
5
1.6k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
890
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
RxSwift コードリーディングの勘所@社内RxSwift勉強会
ukitaka
3
1k
Other Decks in Programming
See All in Programming
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
6
1.2k
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
6
970
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
330
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Jakarta EE meets AI
ivargrimstad
0
250
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
100
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
270
Refactor your code - refactor yourself
xosofox
1
260
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
770
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
3k
Music & Morning Musume
bryan
46
6.2k
Done Done
chrislema
181
16k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
GraphQLとの向き合い方2022年版
quramy
44
13k
Designing for humans not robots
tammielis
250
25k
Side Projects
sachag
452
42k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Cost Of JavaScript in 2023
addyosmani
45
7k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
4 Signs Your Business is Dying
shpigford
181
21k
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(εϨου)Λ มߋ͢Δɻ
Ҏ্ʂʂ