Slide 1

Slide 1 text

RxSwift USAMI Kosuke (Fenrir Inc.)

Slide 2

Slide 2 text

RxSwift ReactiveX for Swift → ReactiveX family : Rx.NET / RxJS / RxJava / ...

Slide 3

Slide 3 text

RxSwift Released! 2.0.0 : 2016/01/02 (Latest release = 2.1.0 : 2016/01/11)

Slide 4

Slide 4 text

5෼ͰΘ͔Δ RxSwift

Slide 5

Slide 5 text

ͱΓ͋͑ͣ࢖ ͬͯΈΔͨΊ ͷ஌ࣝ

Slide 6

Slide 6 text

ͱΓ͋͑ͣ࢖ͬͯΈΔͨΊͷ஌ࣝ → Observable → Subscribe → Error Handling

Slide 7

Slide 7 text

Observable

Slide 8

Slide 8 text

Observable → ඇಉظσʔλετϦʔϜ → UI ͔ΒͷΞΫγϣϯ → OS ͔Βͷ Notification → URL ηογϣϯͷϨεϙϯε

Slide 9

Slide 9 text

Observable ͷཁૉ → Next → Completed → Error

Slide 10

Slide 10 text

Next → case .Next(let value) → value = ετϦʔϜ಺ͷσʔλ

Slide 11

Slide 11 text

Completed → case .Completed → ετϦʔϜͷऴΘΓ → ໌ࣔతͳऴΘΓ͕ͳ͍ετϦʔϜ΋͋Δ

Slide 12

Slide 12 text

Error → case .Error(let error) → error = ErrorType ܕͷ஋ → ετϦʔϜͷऴΘΓͰ΋͋Δ

Slide 13

Slide 13 text

Observable ͷੜ੒ → Observable.create ͳͲͰੜ੒ → RxCocoa ͕༻ҙ͍ͯ͠Δ rx_tap / rx_notification / rx_response / ...

Slide 14

Slide 14 text

Subscribe

Slide 15

Slide 15 text

Subscribe → ετϦʔϜ͔ΒσʔλΛड͚औΔ → subscribeNext → subscribeCompleted → subscribeError

Slide 16

Slide 16 text

ྫ : UIButton ΞΫγϣϯ hogeButton.rx_tap .subscribeNext { [unowned self] _ in self.hoge() }

Slide 17

Slide 17 text

ྫ : NSNotification let center = NSNotificationCenter.defaultCenter() center.rx_notification(UIApplicationDidBecomeActiveNotification) .subscribeNext { [unowned self] _ in self.hoge() }

Slide 18

Slide 18 text

ྫ : NSURLSession let session: NSURLSession = ... let request = NSURLRequest(...) session.rx_response(request) .subscribeNext { (data, response) in ... }

Slide 19

Slide 19 text

Error Handling

Slide 20

Slide 20 text

catchError hogeObservable .catchError { error in ... } .subscribeNext { ... }

Slide 21

Slide 21 text

retry hogeObservable .retry(3) // 3ճ·ͰϦτϥΠ .subscribeNext { ... }

Slide 22

Slide 22 text

retryWhen hogeObservable .retryWhen { _ in // 30ඵޙʹϦτϥΠ return Observable.timer(30, ...) } .subscribeNext { ... }

Slide 23

Slide 23 text

΋͏ͪΐͬͱ Rx Β͘͠

Slide 24

Slide 24 text

΋͏ͪΐͬͱ Rx Β͘͠ → Transforming / Filtering → Combining → Subject

Slide 25

Slide 25 text

Transforming / Filtering

Slide 26

Slide 26 text

Transforming / Filtering → Observable ʹରͯ͠ map / flatMap / filter ͳͲ → Array ʹର͢Δ map / flatMap / filter ͱಉ༷

Slide 27

Slide 27 text

map hogeObservable .map { $0 * 10 }

Slide 28

Slide 28 text

flatMap

Slide 29

Slide 29 text

filter

Slide 30

Slide 30 text

Chaining Operators hogeObservable .filter { ... } .map { ... } .subscribeNext { ... }

Slide 31

Slide 31 text

Combining

Slide 32

Slide 32 text

Combining → ෳ਺ͷετϦʔϜΛͻͱͭͷετϦʔϜʹ·ͱΊ Δ

Slide 33

Slide 33 text

merge

Slide 34

Slide 34 text

combineLatest

Slide 35

Slide 35 text

Subject

Slide 36

Slide 36 text

Subject → Observable ͷҰछ → ֎෦͔Β Subject ʹσʔλΛྲྀͤΔ → Hot Observable

Slide 37

Slide 37 text

σʔλΛྲྀ͢ let subject = PublishSubject() subject.on(.Next("a")) subject.on(.Next("b")) subject.on(.Completed) ////////// subject.subscribeNext { ... }

Slide 38

Slide 38 text

ετϦʔϜͷ෼ذ → ͻͱͭͷετϦʔϜ͔Βෳ਺ͷ Subscriber ʹ → ʢHot Observableʣ

Slide 39

Slide 39 text

͓͞Β͍

Slide 40

Slide 40 text

͓͞Β͍ → Observable → Subscribe → Error Handling → Transforming / Filtering → Combining → Subject

Slide 41

Slide 41 text

Have a Happy RxSwift Life!