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

Kotlin と比較して理解する Swift 5 で実装されるかもしれない async/awa...

yimajo
July 26, 2018

Kotlin と比較して理解する Swift 5 で実装されるかもしれない async/await について

potatotips #53 (iOS/Android開発Tips共有会) in マネーフォワード社で発表した資料です。

yimajo

July 26, 2018
Tweet

More Decks by yimajo

Other Decks in Technology

Transcript

  1. ͜ͷϓϩϙʔβϧʹ͍ͭͯ ಈػίϯϓϦʔγϣϯϋϯυϥ͸࠷దͰ͸ͳ͍ ղܾࡦ͸ίϧʔνϯ 0CK$Πϯϙʔλͷม׵ طଘػೳͱͷ૬ޓ࡞༻ աڈͷίʔυͱͷޓ׵ੑ "#*TUBCJMJUZ΁ͷӨڹ ଞͷγϯλοΫε ୅ସҊ কདྷతͳํ޲ੑ

    w ΫϩʔδϟͰίʔϧόοΫ͢Δͱίʔυ͕ωετ͍ͯͬͯ͠͠·͏ w ΤϥʔॲཧΛϋϯυϥ͝ͱʹॻ͘ͷ͸ࠔ೉ w ৚݅ʹΑͬͯΫϩʔδϟΛݺͼग़͢ͷ͸ۤ௧ w ؔ਺͕஋Λฦ͔͢ɺதஅ͢Δ͜ͱΛՄೳʹ͢Δ w δΣωϨʔλʢZJFMEʣ΍ඇಉظϞσϧͦͷଞͷػೳΛ࣮૷͢Δͷʹ࢖༻Ͱ͖Δ w ͜ͷϓϩϙʔβϧͰ͸ίϯϓϦʔγϣϯϋϯυϥͷଟ͘ͷ໰୊Λഉআ w ࠷΋ҰൠతͳϢʔεέʔεʹ޲໋໊͔ͬͯ๏ͱ༻ޠΛόΠΞε͢Δ w BTZOD͔ZJFMET͔ʹ͍ͭͯ͸ίΞηϚϯςΟΫεʹ͸ؔ܎͠ͳ͍ w ఏҊ͢ΔίϧʔνϯϞσϧ͸ΠϯλϑΣʔεͰ͸ͳ͍ w 0CK$ͷίϯϓϦʔγϣϯϋϯυϥϕʔεͷ"1*Λ 4XJGUͰ࢖͑ΔΑ͏ ม׵͢Δ w BTZODؔ਺΋UISPXTͰ͖ΔΑ͏ʹ͠ɺUSZBXBJUͰϋϯυϦϯάͰ͖ΔΑ͏ʹ͢Δ w ίϯύΠϥ͸Ϋϩʔδϟ͕ඇಉظ͔Ͳ͏͔؆୯ʹ൑ఆͰ͖Δ w EFGFS͸ίϧʔνϯ͕ഁغ͞Εͨࡍʹ΋ಈ࡞͢ΔΑ͏ʹ͢Δ w ʢϋϯυϥͳΒҾ਺Λ૿΍͕͢ʣBTZODͳSFUVSO͸݁ՌΛλϓϧͰฦͤΔ w ίϯύΠϥ͕ANZ2VFVFBTZOD\^Aͳهड़Λݕग़ͯ͠pYJUͰमਖ਼Ͱ͖Δ w "#*4UBCJMJUZʹӨڹ͠ͳ͍ w BTZODͳΒUISPXTͱ͢Δͱύλʔϯ͸ݮΔ͕ܽ఺͸͋Δ w BTZODͳΒσϑΥϧτUISPXTʹ͠ɺBTZOD OPOUISPXJOH ʹ͢Δͷ΋ܽ఺͸͋Δ w ίϧʔνϯΛ༻ҙ͍ͨ͠ͷͰ͋ͬͯBTZODBXBJUΛ࣮૷͍ͨ͠ͷͰ͸ͳ͍ w $ͷΑ͏ʹҰൠతͳBTZODBXBJU͸'VUVSFΫϥεΛฦ͢͜ͱʹͳΔ w ͦ͏ͳΔͱBTZODBXBJUΛݴޠϨϕϧͰ࣮૷͢Δ͜ͱʹͳͬͯ͠·͏ w ͜ͷఏҊ͸AYBXBJUGPP CBS AͷΑ͏ʹνΣΠχϯάΛ͏·͘Ͱ͖Δ w ۩ମྫ͸ͳ͍͕͜ͷఏҊ͔Βকདྷ΁ͷ֦ுੑɺ΍͍͖ͬͯΛड़΂͍ͯΔ
  2. Swift 5ͷυϥϑτʹ͋Δasync/awaitͷྫ // ඇಉظॲཧΛ࣮ߦ͠໭Γ஋Λͦͷ݁Ռͱ͢Δؔ਺ func loadWebResource(_ path: String) async ->

    Resource func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image // ඇಉظॲཧΛ࣮ߦ͠Web্͔ΒσʔλΛऔಘ͠ը૾Λͭ͘Δؔ਺ func processImageData1() async -> Image { let dataResource = await loadWebResource("dataprofile.txt") let imageResource = await loadWebResource(“imagedata.dat") let imageTmp = await decodeImage(dataResource, imageResource) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult }
  3. Swift 5ͷυϥϑτʹ͋Δasync/awaitͷྫ // ඇಉظॲཧΛ࣮ߦ͠໭Γ஋Λͦͷ݁Ռͱ͢Δؔ਺ func loadWebResource(_ path: String) async ->

    Resource func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image // ඇಉظॲཧΛ࣮ߦ͠Web্͔ΒσʔλΛऔಘ͠ը૾Λͭ͘Δؔ਺ func processImageData1() async -> Image { let dataResource = await loadWebResource("dataprofile.txt") let imageResource = await loadWebResource(“imagedata.dat") let imageTmp = await decodeImage(dataResource, imageResource) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult } ؔ਺ఆٛ࣌ʹBTZODΛ͚ͭΔ
  4. Swift 5ͷυϥϑτʹ͋Δasync/awaitͷྫ // ඇಉظॲཧΛ࣮ߦ͠໭Γ஋Λͦͷ݁Ռͱ͢Δؔ਺ func loadWebResource(_ path: String) async ->

    Resource func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image // ඇಉظॲཧΛ࣮ߦ͠Web্͔ΒσʔλΛऔಘ͠ը૾Λͭ͘Δؔ਺ func processImageData1() async -> Image { let dataResource = await loadWebResource("dataprofile.txt") let imageResource = await loadWebResource(“imagedata.dat") let imageTmp = await decodeImage(dataResource, imageResource) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult } ؔ਺ఆٛ࣌ʹBTZODΛ͚ͭΔ BXBJUͰ݁ՌΛ଴ͬͯऔΓग़͢Α͏ʹ هड़Ͱ͖·͢
  5. Kotlin async/awaitͰಉ͡ྫΛهड़ͯ͠ΈΔ fun loadWebResource(resource: String): Deferred<Resource> = async { //

    ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ // @asyncͰDeferredίϯςφʹ͘Δ·ΕΔʁ return@async Resource() } fun processImageData1(): Deferred<Image> = async { val dataResource = loadWebResource("data.txt").await() val imageResource = loadWebResource("imagedata.dat").await() val imageTmp = decodeImage(dataResource, imageResource).await() val imageResult = dewrapAndCleanupImage(imageTmp).await() return@async imageResult }
  6. Kotlin async/awaitͰಉ͡ྫΛهड़ͯ͠ΈΔ fun loadWebResource(resource: String): Deferred<Resource> = async { //

    ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ // @asyncͰDeferredίϯςφʹ͘Δ·ΕΔʁ return@async Resource() } fun processImageData1(): Deferred<Image> = async { val dataResource = loadWebResource("data.txt").await() val imageResource = loadWebResource("imagedata.dat").await() val imageTmp = decodeImage(dataResource, imageResource).await() val imageResult = dewrapAndCleanupImage(imageTmp).await() return@async imageResult } %FGFSSFEΛฦ͢
  7. Kotlin async/awaitͰಉ͡ྫΛهड़ͯ͠ΈΔ fun loadWebResource(resource: String): Deferred<Resource> = async { //

    ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ // @asyncͰDeferredίϯςφʹ͘Δ·ΕΔʁ return@async Resource() } fun processImageData1(): Deferred<Image> = async { val dataResource = loadWebResource("data.txt").await() val imageResource = loadWebResource("imagedata.dat").await() val imageTmp = decodeImage(dataResource, imageResource).await() val imageResult = dewrapAndCleanupImage(imageTmp).await() return@async imageResult } BXBJU ͸%FGFSSFEίϯςφͷϝιου %FGFSSFEΛฦ͢
  8. Kotlin suspendͰಉ͡ྫΛهड़ suspend fun loadWebResource2(): Resource { // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ͍ͯ͠Δײ͡ return

    Resource() } suspend fun processImageData2(): Image { val dataResource = loadWebResource2("data.txt") val imageResource = loadWebResource2("imagedata.dat") val imageTmp = decodeImage2(dataResource, imageResource) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult }
  9. Kotlin suspendͰಉ͡ྫΛهड़ suspend fun loadWebResource2(): Resource { // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ͍ͯ͠Δײ͡ return

    Resource() } suspend fun processImageData2(): Image { val dataResource = loadWebResource2("data.txt") val imageResource = loadWebResource2("imagedata.dat") val imageTmp = decodeImage2(dataResource, imageResource) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult } ؔ਺ଆʹTVTQFOEΩʔϫʔυ෇͚ͯ%FGFSSFEͰͳ͘஋Λฦ͢
  10. Kotlin suspendͰಉ͡ྫΛهड़ suspend fun loadWebResource2(): Resource { // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ͍ͯ͠Δײ͡ return

    Resource() } suspend fun processImageData2(): Image { val dataResource = loadWebResource2("data.txt") val imageResource = loadWebResource2("imagedata.dat") val imageTmp = decodeImage2(dataResource, imageResource) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult } ؔ਺ଆʹTVTQFOEΩʔϫʔυ෇͚ͯ%FGFSSFEͰͳ͘஋Λฦ͢ ݺͼग़͠ଆͰBXBJU͢Δඞཁ͕ͳ͘ͳΔ
  11. ͜ͷॲཧͷແବͳͱ͜Ζ͸ʁ suspend fun loadWebResource2(): Resource { // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ͍ͯ͠Δײ͡ return Resource()

    } suspend fun processImageData2(): Image { val dataResource = loadWebResource2("data.txt") val imageResource = loadWebResource2(“imagedata.dat") val imageTmp = decodeImage2(dataResource, imageResource) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult }
  12. ͜ͷॲཧͷແବͳͱ͜Ζ͸ʁ suspend fun loadWebResource2(): Resource { // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ͍ͯ͠Δײ͡ return Resource()

    } suspend fun processImageData2(): Image { val dataResource = loadWebResource2("data.txt") val imageResource = loadWebResource2(“imagedata.dat") val imageTmp = decodeImage2(dataResource, imageResource) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult } MPBE8FC3FTPVSDF͸ؔ࿈͠ͳ͍ඇಉظॲཧͳͷͰ ͭಉ࣌ʹ࣮ߦͯ݁͠ՌΛ଴ͯ͹ޮ཰͍͍ͷͰ͸ʁ
  13. Kotlin suspendΛasyncͰแΊ͹ෳ਺ॲཧͷ଴ͪ߹ΘͤͰ͖Δ suspend fun processImageData2(): Image { val dataResource =

    async { loadWebResource3(“data.txt”) } val imageResource = async { loadWebResource3(“imagedata.dat”) } val imageTmp = decodeImage2(dataResource.await(), imageResource.await()) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult }
  14. Kotlin suspendΛasyncͰแΊ͹ෳ਺ॲཧͷ଴ͪ߹ΘͤͰ͖Δ suspend fun processImageData2(): Image { val dataResource =

    async { loadWebResource3(“data.txt”) } val imageResource = async { loadWebResource3(“imagedata.dat”) } val imageTmp = decodeImage2(dataResource.await(), imageResource.await()) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult } TVTQFOEͳؔ਺ͦͷ··ΛBTZODͰ͘ΔΜͰ%FGFSSFEʹ
  15. Kotlin suspendΛasyncͰแΊ͹ෳ਺ॲཧͷ଴ͪ߹ΘͤͰ͖Δ suspend fun processImageData2(): Image { val dataResource =

    async { loadWebResource3(“data.txt”) } val imageResource = async { loadWebResource3(“imagedata.dat”) } val imageTmp = decodeImage2(dataResource.await(), imageResource.await()) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult } TVTQFOEͳؔ਺ͦͷ··ΛBTZODͰ͘ΔΜͰ%FGFSSFEʹ %FGFSSFEͳͷͰBXBJU Ͱ͖ΔΑ͏ʹͳͬͨ
  16. Kotlin suspendΛasyncͰแΊ͹ෳ਺ॲཧͷ଴ͪ߹ΘͤͰ͖Δ suspend fun processImageData2(): Image { val dataResource =

    async { loadWebResource3(“data.txt”) } val imageResource = async { loadWebResource3(“imagedata.dat”) } val imageTmp = decodeImage2(dataResource.await(), imageResource.await()) val imageResult = dewrapAndCleanupImage2(imageTmp) return imageResult } TVTQFOEͳؔ਺ͦͷ··ΛBTZODͰ͘ΔΜͰ%FGFSSFEʹ %FGFSSFEͳͷͰBXBJU Ͱ͖ΔΑ͏ʹͳͬͨ ࣮ࡍ͸ٯʹɺ%FGFSSFEϝιουΛTVTQFOEͳϝιουͰ Ͱ͘ΔΉ΄͏͕සൟʹ͔ͭ͏ͩΖ͏ͱ͸ࢥ͍·͢ɻ
  17. Swift 5ͷasync/await΋FutureΛ࢖͑͹ ෳ਺ॲཧͷ଴ͪ߹Θ͕ͤͰ͖Δ func loadWebResource(_ path: String) async -> Resource

    func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image func processImageData1() async -> Image { let dataResource = Future { loadWebResource(“dataprofile.txt") } let imageResource = Future { loadWebResource(“imagedata.dat”) } let imageTmp = await decodeImage(dataResource.get(), imageResource.get()) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult }
  18. Swift 5ͷasync/await΋FutureΛ࢖͑͹ ෳ਺ॲཧͷ଴ͪ߹Θ͕ͤͰ͖Δ func loadWebResource(_ path: String) async -> Resource

    func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image func processImageData1() async -> Image { let dataResource = Future { loadWebResource(“dataprofile.txt") } let imageResource = Future { loadWebResource(“imagedata.dat”) } let imageTmp = await decodeImage(dataResource.get(), imageResource.get()) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult } ࣗલͷ'VUVSFͰ͘ΔΉ
  19. Swift 5ͷasync/await΋FutureΛ࢖͑͹ ෳ਺ॲཧͷ଴ͪ߹Θ͕ͤͰ͖Δ func loadWebResource(_ path: String) async -> Resource

    func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image func processImageData1() async -> Image { let dataResource = Future { loadWebResource(“dataprofile.txt") } let imageResource = Future { loadWebResource(“imagedata.dat”) } let imageTmp = await decodeImage(dataResource.get(), imageResource.get()) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult } ࣗલͷ'VUVSFͰ͘ΔΉ GVUVSFHFUϝιουͰ݁ՌΛ଴ͭ
  20. Swift 5ͷasync/await΋FutureΛ࢖͑͹ ෳ਺ॲཧͷ଴ͪ߹Θ͕ͤͰ͖Δ func loadWebResource(_ path: String) async -> Resource

    func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image func processImageData1() async -> Image { let dataResource = Future { loadWebResource(“dataprofile.txt") } let imageResource = Future { loadWebResource(“imagedata.dat”) } let imageTmp = await decodeImage(dataResource.get(), imageResource.get()) let imageResult = await dewrapAndCleanupImage(imageTmp) return imageResult } ࣗલͷ'VUVSFͰ͘ΔΉ GVUVSFHFUϝιουͰ݁ՌΛ଴ͭ ͭ·Γ,PUMJOͷTVTQFOEʹ͍ۙײ৮
  21. Swift 5 async/await ݚڀಡຊ • ϥΠϒϥϦHydraͰSwift 4Ͱ΋async/awaitΛ࣮ݱ͢Δ • Hydra͕ͲͷΑ͏ʹͯ͠εϨουΛࢭΊ͍ͯΔ͔ղઆ •

    async/await ϓϩϙʔβϧυϥϑτͷղઆ • JS, C#, Dart, KotlinͰͷasync/awaitղઆ IUUQTCPPUIQNKBJUFNT