RxSwiftでエラーを分岐するTips

7d1521d950d9bc5697868830cb04ff2f?s=47 yimajo
November 15, 2018

 RxSwiftでエラーを分岐するTips

potatotips #56 での発表資料です

https://potatotips.connpass.com/event/104242/

7d1521d950d9bc5697868830cb04ff2f?s=128

yimajo

November 15, 2018
Tweet

Transcript

  1. 3Y4XJGUͰΤϥʔΛ෼ذ͢Δ 5JQT ࠓ৓ળۣ!ZJNBKP QPUBUPUJQT4NBSU/FXTࣾ։࠵ ͬͯςΩετ͕ग़͖ͯͨΒͦ͜͸੝Γ্͕ΓϙΠϯτͰ͢ʂ 3FDZDMFE"MVNJOJVN Ҏ߱ͷεϥΠυʹ

  2. ࣗݾ঺հ

  3. J04ΞϓϦ։ൃܥͷٕज़ಉਓࢽΛ#005)Ͱެ։͍ͯ͠·͢ 4XJGUBTZODBXBJUݚڀಡຊ  ԁ IUUQTTXJGUCPPUIQN 

  4. ͞Βʹࠓ݄৽͘͠ެ։͠·ͨ͠ʂ 3Y4XJGUݚڀಡຊᶗ  ԁ IUUQTTXJGUCPPUIQN  3FDZDMFE"MVNJOJVN

  5. ຊ୊

  6. 3Yͷҟৗऴྃͷݪଇ ˣ 0CTFSWBCMFγʔέϯεʹ Τϥʔྲྀ͞ΕΔͱͦΕҎ߱ ߪಡͰ͖ͳ͘ͳΔ ☓ ☓

  7. ҟৗॲཧܥͷύλʔ ϯ͸৭ʑ  ΤϥʔΛΩϟονͯ͠PO&SSPSͱͯ͠ྲྀ͞ͳ͍Α͏ʹ͢Δ w DBUDI&SSPS\^ͯ͠Τϥʔʹରॲ w 0CTFSWBCMF3FTVMUͳܕͰྲྀ͢ w ͜͜Ͱͷ3FTVMU͸੒൱ͱͦͷ৘ใΛؚΉFOVN

     0CTFSWBCMFγʔέϯεΛ෼ذ͢Δ w 0CTFSWBCMF3FTVMUͳܕʹͨ͠΋ͷΛ͞Βʹͭͷγʔέϯεਖ਼ৗͱҟ ৗܥʹ෼ذ w ࢀߟϝϧΧϦΞοςͷ3Y4XJGU࣮૷ΨΠυIUUQTTQFBLFSEFDLDPNQBSBLFFUZ NFSVLBSJBUVUFGBMTFSYTXJGUTIJ[IVBOHHBJEP TMJEF
  8. 0CTFSWBCMFγʔέϯεΛ ਖ਼ৗܥͱΤϥʔͰ෼ذ͢Δͷ͸ 3Y4XJGUͷNBUFSJBMJ[F࢖͑͹ ָʹͰ͖·͢Αͱ͍͏ͷ͕ ࠓճͷ5JQT

  9. ☓ NBUFSJBMJ[F &WFOU &WFOU ☓ &WFOU 3YͷNBUFSJBMJ[F ϝιουͰΠϕϯτΛSFQSFTFOUͨ͠ 0CTFSWBCMF&WFOU5ͳγʔέϯεΛ৽ͨʹ࡞Δ

  10. ਖ਼ৗܥͱҟৗܥͷΠϕϯτΛ औΓग़ͤΔΑ͏ͳϝιουΛ༻ҙ extension ObservableType where E: EventConvertible { public func

    elements() -> Observable<E.ElementType> { return filter { $0.event.element != nil } .map { $0.event.element! } } public func errors() -> Observable<Swift.Error> { return filter { $0.event.error != nil } .map { $0.event.error! } } } // 1. Observable<E>ͷE͕EventConvertible४ڌͰ͋Ε͹࢖͑ΔΑ͏ʹͳΔ // ྫ͑͹ Observable<Event<E>> ͱ͔ // 2_1. Πϕϯτͷਖ਼ৗܥelementͷΈΛฦ͢Α͏ʹ͢Δ͚ͩ // 2_2. ΠϕϯτͷΤϥʔͷΈΛฦ͢Α͏ʹ͢Δ͚ͩ
  11. ར༻ྫ

  12. let result = Observable.of(1, 2) .flatMap { _ -> Observable<Event<String>>

    in let observable = Observable<String>.create { observer in observer.onNext("A") observer.onError(TestError.test) observer.onNext(“B") observer.onCompleted() return Disposables.create() } return observable.materialize() } output1 = result .elements() .subscribe(onNext: { print("elements, onNext: \($0)”) }, onError: { print("elements, onError: \($0)") },) error = result .errors() .subscribe(onNext: { print("errors, onNext: \($0)") }, onError: { print("errors, onError: \($0)") }) —— ग़ྗ —— elements, onNext: A elements, onNext: A elements, onCompleted: errors, onNext: test errors, onNext: test errors, onCompleted:
  13. ͱ͍͏͜ͱͰ FMFNFOUT ͱFSSPST ϝιουΛFYUFOTJPOʹ͠ ର৅ͷ0CTFSWBCMFγʔέϯεʹ NBUFSJBMJ[F ͢Δ͚ͩͰ FSSPSͷγʔέϯε͕෼ذͰ͖Δʜ

  14. 7JFX.PEFM MFUPVUQVU%SJWFS#PPM MFUFSSPS%SJWFS&SSPS ͔͠͠ &SSPSܕͬͯΧόʔൣғ޿͗͢ΔͷͰ͸ʁ ࣗલͰΤϥʔ෼ذͯ͠ΔΜ͔ͩΒܕΛ੍ݶ͢Ε͹͍͍ͷʹ

  15. ྫಛఆͷΤϥʔʹ͢Δ let error: Observable<OAuthSwiftError> = result .errors() .map { guard

    let error = $0 as? OAuthSwiftError else { // ͜͜ͰΫϥογϡϨϙʔτૹΓͭͭ …. fatalError($0.localizedDescription) } return error } .share(replay: 1) w Ͱ͖Δ͚Ͳ΋ʜ w ΍Δඞཁ͕͋ΔͱࢥͬͨΒ΍ͬͨΒΑΖͦ͠͏ w .PEFM૚ͷԞਂ͘ͰಛఆͷΤϥʔͷΈΛ૝ఆ͍ͯ͠Δ৔߹ͳͲ w ͦ͜·Ͱ΍Δඞཁͳͦ͞͏ͱ൑அͰ͖Δͱ͖ w 7JFX.PEFMͷϓϨθϯςʔγϣϯϩδοΫͰ&SSPS൑ผ͢Ε͹͍ ͍ͱ͍͏৔߹ͳͲ
  16. ͔͠͠ࠔͬͨ͜ͱʹ

  17. 4JOHMF΍.BZCF $PNQMFUBCMFͰ͸ ͦͷ··Ͱ͸ NBUFSJBMJ[F͸࢖͑ͳ͍

  18. 1SPUPDPM 0CTFSWBCMF5ZQF NBUFSJBMJ[F 1SPUPDPM 1SJNJUJWF4FRVFODF5ZQF $MBTT 0CTFSWBCMF& 1SPUPDPM 0CTFSWBCMF$POWFSUJCMF5ZQF 4USVDU

    1SJNJUJWF4FRVFODF5SBJU &MFNFOU 4JOHMFͳͲ
  19. 4JOHMFͰNBUFSJBMJ[FΛ࢖͍͍ͨ৔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