Save 37% off PRO during our Black Friday Sale! »

RxSwiftでTwitterの
ライブラリを作ってる話 / RxTwift

C0e58802a4228552548bb8ca596ca966?s=47 Mihyaeru
March 12, 2016

RxSwiftでTwitterの
ライブラリを作ってる話 / RxTwift

RxSwiftでTwitterの
ライブラリを作ってる話

C0e58802a4228552548bb8ca596ca966?s=128

Mihyaeru

March 12, 2016
Tweet

Transcript

  1. RxSwiftͰTwitterͷ
 ϥΠϒϥϦΛ࡞ͬͯΔ࿩ (͍͍ͩͨ)৽ଔΤϯδχΞ޲͚ٕज़ަྲྀձ vol.6 on 2016/03/12 @mihyaeru21

  2. ͓લɺ୭Α • ϛώϟΤϧ @mihyaeru21 • Swift͓͡͞Μ • Kotlin͓͡͞Μ • ͱ͖Ͳ͖PerlϚϯ

  3. RxSwift + Twitter

  4. RxTwift

  5. RxTwift • RxSwiftϕʔε • ґଘؚΊͯSwiftͷΈ • ܕܕܕ • https://github.com/mihyaeru21/RxTwift

  6. ͖ͯͱ͏ͳઆ໌ • TwitterͷAPIͱରԠͨ͠ΠϯλϑΣʔε • ApiΫϥε͸ɺ͍ͣΕRestApiʹ໊લ͕มΘΓͦ͏ let api = RxTwift.Api(…) api.get.statuses.homeTimeline()

    GET /statuses/home_timeline
  7. ͖ͯͱ͏ͳઆ໌ • શͯͷAPIϝιου͸ObservableΛฦ͢ api.get.statuses.homeTimeline() Observable<Tweet>

  8. ͖ͯͱ͏ͳઆ໌ • Observableͷத਎͸JSONͰ͸ͳ͍ΦϒδΣΫτ api.get.statuses.homeTimeline() .subscribeNext { tweet in print("\(tweet.user.name)\n\(tweet.text)") }

  9. ͖ͯͱ͏ͳઆ໌ • Observableͷத਎͸JSONͰ͸ͳ͍ΦϒδΣΫτ api.get.statuses.homeTimeline() .subscribeNext { tweet in print("\(tweet.user.name)\n\(tweet.text)") }

    ίʔυิ׬Ͱ͖Δ޾ͤʂʂʂ
  10. ͪΐͬͱෳࡶͳྫ • ͋ΔϦετΛऔಘͯ͠ • ͦͷϦετ಺ͷϢʔβʢσϑΥϧτ͸20ਓʣ • ͷ͏ͪɺID͕ۮ਺ͷਓͷʢҙຯෆ໌ͳૢ࡞͚ͩͲྫ͔ͩΒڐͯ͠ʣ • λΠϜϥΠϯΛऔಘͯ͠ʢAPIΛୟ͖·͘Δʣ •

    දࣔ
  11. Θʔɺ͍͢͝

  12. த਎ͷ࿩

  13. த਎ͷ࿩ • OAuth • ΦϒδΣΫτϚοϐϯά • Rx • ެ։APIϝιουͷྫ

  14. OAuth • OAuthΛSwiftͰ࣮૷͍ͯ͠ΔϥΠϒϥϦ͸͋Δ • https://github.com/OAuthSwift/OAuthSwift • ͦΜͳʹଟػೳ͸ඞཁͳ͔ͬͨ • ετϦʔϛϯάʹରԠͰ͖ͳͦ͏ •

    ͱ͍͏Θ͚Ͱࣗ࡞ • HMAC-SHA1͚ͩCryptoSwiftʹґଘ • https://github.com/krzyzanowskim/CryptoSwift • ඞཁͳػೳΑΓଟػೳ͗͢Δ͔Βґଘ͔Β֎͍ͨ͠
  15. OAuth: ೝূͷϔομΛ࡞Δ෦෼

  16. OAuth • Ϧετॲཧͱ͔จࣈྻॲཧ͕׬݁ʹॻ͖΍͍͢ • ʢൺֱର৅ɿObjective-Cʣ • ྫ֎ॲཧ͸nilΛฦ͚ͩ͢ͷखൈ͖ • खൈ͖͚ͩͲ͜Ε͘Β͍ͷॲཧʹ͸े෼

  17. ΦϒδΣΫτϚοϐϯά • Argo: https://github.com/thoughtbot/Argo • Swift޲͚ϥΠϒϥϦͰ࠷΋γϯϓϧʢͳίʔυʹͳΔ

  18. ΦϒδΣΫτϚοϐϯά • README͔ΒҾ༻ struct User { let id: Int let

    name: String let email: String? let role: Role let companyName: String let friends: [User] }
  19. ΦϒδΣΫτϚοϐϯά • README͔ΒҾ༻ extension User: Decodable { static func decode(j:

    JSON) -> Decoded<User> { return curry(User.init) <^> j <| "id" <*> j <| "name" <*> j <|? "email" <*> j <| "role" <*> j <| ["company", "name"] <*> j <|| "friends" } }
  20. ΦϒδΣΫτϚοϐϯά • มͳه߸ͱ͔ʹ͍ͭͯ͸ྑ͍ղઆهࣄ͕͋Δ • http://qiita.com/koher/items/300d89136b515291dac4

  21. ΦϒδΣΫτϚοϐϯά

  22. ΦϒδΣΫτϚοϐϯά ͕ࣜෳࡶ͗ͯ͢ίϯύΠϧͰ͖Ͷʔ͔Β ෼ׂͯͪ͠ΐ

  23. ΦϒδΣΫτϚοϐϯά ࠅ͍͚Ͳಈ͘…

  24. Rx • RxSwift • https://github.com/ReactiveX/RxSwift • ࠷ۙྲྀߦΓʁͷFRP • http://ninjinkun.hatenablog.com/entry/introrxja

  25. Rx • Observable • 1ͭͷPromiseʹෳ਺ͷ஋ΛྲྀͤͯߴػೳԽͨ͠ײ͡ • ObservableΛ࡞ͬͨஈ֊Ͱ͸Կ΋͠ͳ͍ • ୭͔ʹsubscribe͞ΕΔ͜ͱʹΑͬͯىಈ

  26. Rx: RxTwiftͰ͸ • APIڞ௨ͷϦΫΤετൃߦ෦෼ • Observable<NSData>

  27. Rx: RxTwiftͰ͸ • JSONจࣈྻΛදݱ͢ΔNSDataΛσίʔυ • ArgoʹΑͬͯΦϒδΣΫτʹม׵ • Observable<Tweet> • Observable<User>

    • etc…
  28. ެ։APIϝιουͷ࣮૷ྫ

  29. ެ։APIϝιουͷ࣮૷ྫ

  30. client.get(), client.post() • RxTwiftʹ͓͚ΔObservableͷݯྲྀ • Observable.create()ʹΑͬͯੜ੒ • த਎͸ෳࡶ͔ͩΒׂѪ

  31. ެ։APIϝιουͷ࣮૷ྫ

  32. Dictionary.createWithNotNil • nilͳཁૉΛল͍ͨDictionaryΛ࡞Δ

  33. ެ։APIϝιουͷ࣮૷ྫ

  34. Observable<Element>.decode()

  35. ฦΓ஋ͷܕͰΦʔόʔϩʔυ ҧ͍͸͜Ε͚ͩ

  36. Ϛοϐϯάର৅ͷܕ͸ฦΓ஋͔Βਪ࿦

  37. Ϛοϐϯάର৅ͷܕ͸ฦΓ஋͔Βਪ࿦ ͜ΕʹΑͬͯͲͷΦϒδΣΫτ
 ʹϚοϐϯά͢Δ͔ܾ·Δ

  38. Ϛοϐϯάର৅ͷܕ͸ฦΓ஋͔Βਪ࿦ ͜ΕʹΑͬͯͲͷΦϒδΣΫτ
 ʹϚοϐϯά͢Δ͔ܾ·Δ

  39. Observable<Element>.flatMapSequence() • Observable<[T]> Λ Observable<T> ʹม׵ • ετϦʔϜͱͯ͠ѻ͍΍ͯ͘͢͠Δ • ʢ͜ͷม׵͸ແ͔͘͢΋…ʣ

  40. decode()ͷฦΓ஋ͷਪ࿦ • flatMapSequence() • Observable<[Tweet]> -> Observable<Tweet> • decode() •

    Observable<NSData> -> Observable<[Tweet]>
  41. ݱঢ়Ͱ͖͍ͯΔൣғ • OAuthೝূʹΑΔ௨৴ • GET/POST status/* ͷAPI • GET lists/*

    ͷAPI • ࣮૷ࡁΈAPIͷϨεϙϯεʹରԠ͢ΔΦϒδΣΫτ • TwitterଆΤϥʔʹରԠ͢ΔΦϒδΣΫτ
  42. ࠓޙ࣮૷͢Δൣғ • ೝূͷτʔΫϯऔಘॲཧ • ະ࣮૷ͷREST API • ετϦʔϛϯάAPI • ύϥϝʔλʹԠͯ͡ϨεϙϯεJSONͷߏ଄͕


    มΘΔ͜ͱ΁ͷରԠ • ະ࣮૷ͷΦϒδΣΫτ