Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Concurrencyの 尻尾を掴め ~ 並行処理を受け入れる取っ掛かり

Swift Concurrencyの 尻尾を掴め ~ 並行処理を受け入れる取っ掛かり

notoroid

March 18, 2022
Tweet

More Decks by notoroid

Other Decks in Programming

Transcript

  1. ೳొཁ !OPUPSPJE J1IPOFΞϓϦ։ൃऀ೥݄೔ 4XJGU$PODVSSFODZͷ ৲ඌΛ௫Ί ʙฒߦॲཧΛड͚ೖΕΔऔֻ͔ͬΓ

  2. ࿩͢಺༰ w 4XJGU$PODVSSFODZͷେ࿮ w 4XJGU$PODVSSFODZͷऔֻ͔ͬΓΛ௫ΉͨΊͷଊ͑ํͭ

  3. ࿩͞ͳ͍಺༰ w BDUPSͷৄࡉ w !.BJO"DUPS w !HMPCBM"DUPS w ߏ଄Խ͞Εͨฒߦੑ BDUPS·Ͱ࿩͢ʹ͸

    BDUPS͸ॏཁ͕ͩ ͕࣌ؒ଍Γͳ͍ʜ
  4. ͓୊໨ w ظ଴ͱͨΊଉͷ4XJGU$PODVSSFODZ w ؆ܿʹ4XJGU$PODVSSFODZͷઆ໌ w ֆͰऔֻ͔ͬΓΛ͔ͭΉ w ୹͍จࣈྻͰࣔͯ͠ΈΔ w

    ̐֊૚Ͱࣔͯ͠ΈΔ w ·ͱΊ
  5. ຊ୊ʹೖΔલʹ

  6. BlogͰ2020೥ʹ౤ߘͨ͠಺༰

  7. "QQMFϚϧνϓϥοτϑ ΥʔϜ .VMUJQMBUGPSN  ରԠ͸ෳ਺ͷJOGPQMJTU ΁ͷରԠͰ΋͋Δ࿩ J04 J1BE04 NBD04 IUUQTJSJNBTVDPN

    BQQMFNVMUJQMBUGPSNJOGP QMJTUJPTJQBEPTNBDPT
  8. w ೥౰࣌ɺNVMUJQMBUGPSNΛᨳ͍ͳ͕ΒJOGPQMJTU͚͕ͩऔΓ࢒͞Ε͍ͯΔ ఺Λࢦఠ w 9DPEFҎ߱Ͱ͸JOGPQMJTUͷ֤छ߲໨͸ϓϩδΣΫτઃఆ͔Β(FOFSBUF͞ ΕΔΑ͏ʹͳ͍ͬͯΔ

  9. JOGPQMJTU w ೥౰࣌ɺNVMUJQMBUGPSNΛᨳ͍ͳ͕ΒJOGPQMJTU͚͕ͩऔΓ࢒͞Ε͍ͯΔ ఺Λࢦఠ w 9DPEFҎ߱Ͱ͸JOGPQMJTUͷ֤छ߲໨͸ϓϩδΣΫτઃఆ͔Β(FOFSBUF͞ ΕΔΑ͏ʹͳ͍ͬͯΔ

  10. ࣮࣭"QQMF։ൃ؀ڥͷมԽΛ ༧૝͍ͯ͠Δ

  11. ͓ͦΒ͘4XJGU6*Ͱ8FCΞϓϦΛ ࡞੒Ͱ͖Δ೔΋͍ۙ͸ͣ

  12. ؓ࿩ٳ୊

  13. ظ଴ͱͨΊଉͷ4XJGU $PODVSSFODZ

  14. ظ଴ͱͨΊଉͷ4XJGU$PODVSSFODZ w 4XJGU͕ൃද͞Εͯ೥ 88%$ʙ  w "QQMF͕৽ݴޠΛಋೖͨ͠໨࿦ݟ͸ॾʑ w େ੎ྗͱͳͬͨ8FC։ൃऀΛऔΓࠐΈ͍ͨͱ͍͏ҙਤ͸͋Δ

  15. Կނ

  16. େ͖͍։ൃऀूஂ͔ΒΞϓϦ։ൃऀΛҾ͖د͍ͤͨ "OESPJE͕+BWBݴޠΛཧղͰ͖ΔϓϩάϥϚʔऔΓࠐΜͰେ͖͘ͳͬͨྺ࢙Λ࠶ݱ͍ͨ͠

  17. 4XJGU͕ औΓࠐΈ͍ͨ࠷ۙͷ &$."4DSJQU

  18. &$."4DSJQU ೥୆ޙ൒ʙ೥୆લ൒ αʔόʔαΠυͰ/PEF+4࠾༻ྫ͕૿͔͑ͯΒٕज़తվળ͕Ճ଎ ॱෆಉ  /PEF+4%FOP ύοέʔδ؅ཧ   #BCFM

    ԼҐޓ׵ੑΛҡ࣋͢Δτϥϯείʔμʔ   ؆ܿͳهड़ Ξϩʔؔ਺ࣜ ͷ௥Ճ #BCFMʹΑΔԼҐޓ׵อͪͭͭಋೖ   &4-JOU ੩తߏจղੳ͕࣮࣭ͩϏϧυॲཧͷҰ෦   QSPNJTF BTZODBXBJUಉظॲཧ   BTZODBXBJU ؆ܿͳಉظॲཧهड़
  19. 8FC"TTFNCMZ ϑϩϯτΤϯυͰ͸ॲཧͷߴ଎ԽΛ໨ࢦͨ͠WebԾ૝Ϛγϯͱͦͷ্Ͱಈ࡞͢ΔόΠφϦʔ͕ొ৔ɻ

  20. BTZODBXBJU͕&$."4DSJQU্Ͱ࣮ ݱ͞ΕͨࣄͰBTZODBXBJU͕ҰൠԽ

  21. 4XJGU΋4XJGU$PODVSSFODZʹؚΊΔܗͰ BZODBXBJUهड़Λಋೖ

  22. ؆ܿʹ4XJGU$PODVSSFODZͷه๏

  23. Swift Concurrencyͷ؆ܿͳهड़ʹ(࠷ॳ͸)جͮ͘ɻiOS15 SDKͰಋೖ͞ΕͨURLSessionͷSwift Concurrency޲͚ͷϢʔςΟϦςΟϝιουΛ༻͍Δɻ let (data, response) = await URLSession.shared.data(for:

    ϦΫΤετ)
  24. Swift Concurrencyͷ؆ܿͳهड़ʹ(࠷ॳ͸)جͮ͘ɻiOS15 SDKͰಋೖ͞ΕͨURLSessionͷSwift Concurrency޲͚ͷϢʔςΟϦςΟϝιουΛ༻͍Δɻ let (data, response) = await URLSession.shared.data(for:

    ϦΫΤετ) ͨͩ͠ɺECMAScriptͱ͸ҟͳΓɻtry/throw ػߏ͕ߟྀ͞Ε͍ͯΔඞཁ͕͋Δɻ let (data, response) = try await URLSession.shared.data(for: ϦΫΤετ)
  25. Swift Concurrencyͷ؆ܿͳهड़ʹ(࠷ॳ͸)جͮ͘ɻiOS15 SDKͰಋೖ͞ΕͨURLSessionͷSwift Concurrency޲͚ͷϢʔςΟϦςΟϝιουΛ༻͍Δɻ let (data, response) = await URLSession.shared.data(for:

    ϦΫΤετ) ͨͩ͠ɺECMAScriptͱ͸ҟͳΓɻtry/throw ػߏ͕ߟྀ͞Ε͍ͯΔඞཁ͕͋Δɻ let (data, response) = try await URLSession.shared.data(for: ϦΫΤετ) ͨͩ͠ɺTaskͰғΉ(΋͘͠͸TaskΛ಺แͨ͠ϝιουex: SwiftUI ͷtask) Λ௥Ճ͢Δඞཁ͕͋Δɻ Task { let (data, response) = try await URLSession.shared.data(for: ϦΫΤετ) }
  26. struct RandomFox: Equatable, Codable { let image: String; let link:

    String} Task { let url = URL(string: "https://randomfox.ca/floof/")! let request = URLRequest(url: URL(string: "https://randomfox.ca/ floof")!, cachePolicy: .reloadIgnoringLocalCacheData) do { let (data, response) = try await URLSession.shared.data(for: request) _ = response as! HTTPURLResponse let randomFox = try JSONDecoder().decode(RandomFox.self, from: data) } catch error { print("error=\(error.localizedDescription)") } }
  27. λεΫͬͯԿ

  28. ฏߦॲཧͷྲྀΕΛҰखʹ Ҿ͖ड͚Δͷ͕5BTL

  29. ݹయతฏߦॲཧ ϝΠϯεϨου଴ͪεϨουϫʔΧʔεϨου ϫʔΧʔεϨου ଴ͪεϨου ϝΠϯεϨου Ωϟϯηϧ΋Ͱ͖Δ ࡞ۀ୲౰εϨου ࡞ۀΛ؂ࢹ͢Δ ݁ՌΛฦ͢ ݁ՌΛฦ͢

  30. ݹయతฏߦॲཧ ϝΠϯεϨου଴ͪεϨουϫʔΧʔεϨου ϫʔΧʔεϨου ଴ͪεϨου ϝΠϯεϨου Ωϟϯηϧ΋Ͱ͖Δ ࡞ۀ୲౰εϨου ࡞ۀΛ؂ࢹ͢Δ ݁ՌΛฦ͢ ݁ՌΛฦ͢

    5BTL 5BTL͕ड͚࣋ͭ 5BTL͕ड͚࣋ͭ 5BTL͕ड͚࣋ͭ
  31. ๬Μͩ͸͕ͣͩͬͨ

  32. BTZODBXBJUBDUPS5BTL!.BJO"DUPS !HMPCBM"DUPS!4FOEBCMF!VODIFDLFE 5BTLEFUBDIFEXJUI$IFDLFE$POUJOVBUJPO XJUI$IFDLFE5ISPXJOH$POUJOVBUJPO XJUI6OTBGF$POUJOVBUJPO XJUI6OTBGF5ISPXJOH$POUJOVBUJPOXJUI5BT XJUI5ISPXJOH5BTL(SPVQ5BTLJT$BODFMMFE $BODFMMBUJPO&SSPSXJUI5BTL$BODFMMBUJPO)B ಘΒΕͨͷ͸େྔͷΩʔϫʔυ

  33. ͕࣌ؒܦա͠ ެࣜҎ֎ͷυΩϡϝϯτ͕ॆ࣮த

  34. ެࣜҎ֎ͷυΩϡϝϯτ ެ։͞Εͯ೥ܦա͠Α͏ͱ͢Δதɺ༏लͳެࣜҎ֎ͷυΩϡϝϯτ͕ొ৔ w 4XJGU$PODVSSFODZ·ͱΊʢਖ਼ࣜ൛ରԠࡁʣ w IUUQT[FOOEFWBLLZJFBSUJDMFTTXJGUDPODVSSFODZ w 4XJGU$PODVSSFODZνʔτγʔτ w IUUQT[FOOEFWLPIFSBSUJDMFTTXJGUDPODVSSFODZDIFBUTIFFU

    w 4XJGU$PODVSSFODZͷXJUI5BTL$BODFMMBUJPO)BOEMFSͱ4FOEBCMFDPDLTDPNCMPH  w IUUQTDPDLTDPNCIBUFOBCMPHDPNFOUSZ w .PEFSO$PODVSSFODZJO4XJGU*OUSPEVDUJPOw"OEZ*CBOF[ w IUUQTXXXBOEZJCBOF[DPNQPTUTNPEFSODPODVSSFODZJOTXJGUJOUSPEVDUJPO
  35. ͦΕͰ΋औΓ૊Ήʹ͸ Կ͔͠Βͷऔֻ͔ͬΓ͕ඞཁ

  36. 4XJGU$PODVSSFODZͷ ෳࡶ͔͞Βଊ͑ͯΈΔ

  37. ୹͍จࣈྻͰࣔͯ͠ΈΔ

  38. B5X T

  39. B5X T BZODBXBJUBDUPS 5BTL 8JUIʙͳͲେྔͷϝιου܈

  40. B5X T BZODBXBJUBDUPS 5BTL XJUIʙͳͲେྔͷϝιου܈ 4XJGU༧໿ޠ ࣮͸ػೳͷࠜװ ෳࡶͳػೳ܈ γϯϓϧͰ੔ཧ͞Εͨهड़ ৑௕Ͱෳࡶ

    ࢖͍͜ͳ͢ʹ͸ඞཁ 5BTLͷதͰ͔͠ػೳ͠ͳ͍
  41. Կނ͜͜·Ͱෳࡶ

  42. "QQMFͱͯ͠͸ ͋ΒΏΔେن໛ͳ࢓૊Έ·Ͱద༻͍ͨ͠

  43. େن໛ͱ͸ྫ͑͹

  44. $PSF$16

  45. ڧྗͳ$16ͷύϫʔΛϑϧՔಇ ͤ͞Δͷ͸"QQMFͷࢸ্໋୊

  46. "QQMFۘ੡Ͱͳ͚Ε͹ಋೖΛ᪳᪯͢Δ ن໛ͷπʔϧΩοτ܈ͱԽ͍ͯ͠Δ

  47. ̐֊૚Ͱࣔͯ͠ΈΔ

  48. BZODBXBJUΛؾܰ ʹݺͼग़ͤΔ֊૚ "ZODBXBJUΛ࢖ͬ ͯػೳ௥Ճ͢Δ֊૚ J04Ҏ্ J04Ҏ্ BTZODBXBJU 5BTL BDUPS ͦͷଞ4XJGU

    $PODVSSFODZػೳ w 4XJGU w 4XJGU"UUSJCVUFT w (SBOE$FOUSBM%JTQBUDI w ڧྗͳϓϩηοαʔͷྗΛҾ͖ग़͢ w ෆ۩߹ͷೖΓࠐΈͮΒ͍ฏߦॲཧ લఏͱͳΔٕज़ "QQMFͷҙਤ
  49. BZODBXBJUΛؾܰ ʹݺͼग़ͤΔ "ZODBXBJUΛ࢖ͬͯ ػೳ௥Ճ͢Δʹ͸ඞཁ J04Ҏ্ J04Ҏ্ BTZODBXBJU 5BTL BDUPS ͦͷଞ4XJGU

    $PODVSSFODZػೳ w 4XJGU w 4XJGU"UUSJCVUFT w (SBOE$FOUSBM%JTQBUDI w ڧྗͳϓϩηοαʔͷྗΛҾ͖ग़͢ w ෆ۩߹ͷೖΓࠐΈͮΒ͍ฏߦॲཧ લఏͱͳΔٕज़ "QQMFͷҙਤ εϨουΛӅṭͰ͖͍ͯΔൣғ
  50. 4XJGU"UUSJCVUFT͸ҎԼΛࢀর IUUQTJSJNBTVDPNBQQMF TXJGUBUUSJCVUFT

  51. ΋ͬͱࠜఈͷ໰୊΋͋Δ

  52. ࢛൒ੈلલʹ౤͛ೖΕΒΕͨϚϧνεϨου ίϯγϡʔϚʔ04ʹϚϧνεϨου͕౤͛ࠐ·Ε࢛ͯ൒ੈل w 8JOEPXT͕ίϯγϡʔϚʔ04ͱͯ͠εϨουΛಋೖ w εϨουಋೖ͸ϚʔέςΟϯάతͳҙຯ͕ڧ͔ͬͨ w σʔλΛอޢͭͭ͠ฒߦͯ͠ಈ͘ΞΠσΞ͸ະख़ͷ··ϦϦʔε w ౰࣌ͷϓϩάϥϛϯάݴޠ

    ౰࣌ͷ$$ ͸ϚϧνεϨουಈ࡞͸࢓༷ʹؚΊ ͣ w Ҏޙͷݴޠ΋ϚϧνεϨουʹܾؔͯ͠ఆଧ͸ଧͪग़ͤͣ w ͨͩ͠.JDSPTPGU͸BTZODBXBJUͷಋೖࣗମ͸ૣ͔ͬͨ $ 5ZQF4DSJQU
  53. 4XJGU$PODVSSFODZ͕ղܾ͠Α͏ͱ͍ͯ͠Δ΋ͷ  ࢛൒ੈلલʹ์Γग़͞Εͨ ϚϧνεϨουͷऔΓѻ͍Λ༰қʹ͢Δ  ྫ֎ॲཧ USZʙDBUDI ΛฒߦॲཧͰ΋҆શʹߦ͏  ϞμϯͳϓϩάϥϛϯάݴޠͷϧοΫε΁ͷ௥ਵ

    ։ൃऀͷؒޱΛ֦͛Δ
  54. ·ͱΊ

  55. ·ͱΊ w 4XJGU$PODVSSFODZൃද͞Ε͔ͯΒ೥ۙ͘ܦͪ·ͨ͠ w ༗༻ͳࢿྉ͸ग़͖ͯ·͕ͨ͠࢓༷͕ڊେ͗ͯ͢औֻ͔ͬΓ͕গͳ͍Ͱ͢ w ߟҊ͍͔ͨͭ͘͠ͷऔֻ͔ͬΓΛઆ໌͠·ͨ͠ w B5X T

     w 4XJGU$PODVSSFODZͷཧղ֊૚
  56. J045FDI#MPHߋ৽த w4XJDU6*Λ࢖ͬͯ̍̌̌ߦͰαϯϓϧίʔυΛॻ͘ 4XJGUߦγϦʔζଞ"QQMF։ൃؔ܎ͷهࣄΛܝࡌத Ͱ͢ɻ JSJNBTVDPN OPUFDPNOPUPSPJE wʹ΋هࡌதͰ͢ɻ

  57. ࢀߟ w 4XJGU$PODVSSFODZ·ͱΊʢਖ਼ࣜ൛ରԠࡁʣ w IUUQT[FOOEFWBLLZJFBSUJDMFTTXJGU DPODVSSFODZ w 4XJGU$PODVSSFODZνʔτγʔτ w IUUQT[FOOEFWLPIFSBSUJDMFTTXJGU

    DPODVSSFODZDIFBUTIFFU w 4XJGU$PODVSSFODZͷXJUI5BTL$BODFMMBUJPO)BOEMFS ͱ4FOEBCMFDPDLTDPNCMPH  w IUUQTDPDLTDPNCIBUFOBCMPHDPNFOUSZ  w .PEFSO$PODVSSFODZJO4XJGU*OUSPEVDUJPOw "OEZ*CBOF[ w IUUQTXXXBOEZJCBOF[DPNQPTUTNPEFSO DPODVSSFODZJOTXJGUJOUSPEVDUJPO w "TZODBXBJU8JLJQFEJB w IUUQTFOXJLJQFEJBPSHXJLJ"TZODBXBJU w UISFBEɺDPSPVUJOFͷྫ֎Λసૹ͢Δʂʂ2JJUB w IUUQTRJJUBDPN:VLJ.JZBUBLFJUFNT EEEFFEB w <ϓϨθϯࢿྉ> "QQMF͕ ΍Γ͍ͨ์୊4XJGU "UUSJCVUFTc*SJNBTV%FOTBO1MBOOJOH͍Γ·͢ి ࢉاը w IUUQTJSJNBTVDPNBQQMFTXJGUBUUSJCVUFT