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

    ԼҐޓ׵ੑΛҡ࣋͢Δτϥϯείʔμʔ   ؆ܿͳهड़ Ξϩʔؔ਺ࣜ ͷ௥Ճ #BCFMʹΑΔԼҐޓ׵อͪͭͭಋೖ   &4-JOU ੩తߏจղੳ͕࣮࣭ͩϏϧυॲཧͷҰ෦   QSPNJTF BTZODBXBJUಉظॲཧ   BTZODBXBJU ؆ܿͳಉظॲཧهड़
  2. 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: ϦΫΤετ)
  3. 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: ϦΫΤετ) }
  4. 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)") } }
  5. ެࣜҎ֎ͷυΩϡϝϯτ ެ։͞Εͯ೥ܦա͠Α͏ͱ͢Δதɺ༏लͳެࣜҎ֎ͷυΩϡϝϯτ͕ొ৔ 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
  6. BZODBXBJUΛؾܰ ʹݺͼग़ͤΔ֊૚ "ZODBXBJUΛ࢖ͬ ͯػೳ௥Ճ͢Δ֊૚ J04Ҏ্ J04Ҏ্ BTZODBXBJU 5BTL BDUPS ͦͷଞ4XJGU

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

    $PODVSSFODZػೳ w 4XJGU w 4XJGU"UUSJCVUFT w (SBOE$FOUSBM%JTQBUDI w ڧྗͳϓϩηοαʔͷྗΛҾ͖ग़͢ w ෆ۩߹ͷೖΓࠐΈͮΒ͍ฏߦॲཧ લఏͱͳΔٕज़ "QQMFͷҙਤ εϨουΛӅṭͰ͖͍ͯΔൣғ
  8. ࢀߟ 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