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

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

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. Kotlin ͱൺֱͯ͠ཧղ͢Δ
    Swift 5 Ͱ࣮૷͞ΕΔ͔΋͠Εͳ͍
    async/await
    ʹ͍ͭͯ
    גࣜձࣾΩϡϦΦγςΟιϑτ΢ΣΞ ࠓ৓ ળۣ @yiamjo
    QPUBUPUJQTJOϚωʔϑΥϫʔυࣾ

    View full-size slide

  2. Swift 5 async/await ݚڀಡຊ
    IUUQTCPPUIQNKBJUFNT
    0QUJPOBM ٕज़ಉਓࢽΛ࡞Γ·ͨ͠

    View full-size slide

  3. ݱࡏͷSwift 4.1͸
    async/awaitΛαϙʔτ͍ͯ͠ͳ͍
    - Xcode 10ͷSwift 4.2Ͱ΋async/await͸ͳ͍

    View full-size slide

  4. 2017/8 SwiftͷੜΈͷ਌ΫϦεࢯ͔Β
    async/awaitϓϩϙʔβϧͷυϥϑτ͕
    Swift EvolutionͷMLʹ౤ߘ͞ΕΔ

    View full-size slide

  5. ͜ͷϓϩϙʔβϧʹ͍ͭͯ
    ಈػίϯϓϦʔγϣϯϋϯυϥ͸࠷దͰ͸ͳ͍
    ղܾࡦ͸ίϧʔνϯ
    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 ۩ମྫ͸ͳ͍͕͜ͷఏҊ͔Βকདྷ΁ͷ֦ுੑɺ΍͍͖ͬͯΛड़΂͍ͯΔ

    View full-size slide

  6. - 2017/8 Swift EvolutionͷϦϙδτϦʹSwift 5
    ͷ༧ఆͱͯ͠ɺඇಉظॲཧͷ৽͍͠جૅΛ࡞
    Δͱ͍͏͜ͱ͕௥ه͞Εͨ
    - https://github.com/apple/swift-evolution

    View full-size slide

  7. ઌʹൃදͷ݁࿦Λ·ͱΊΔ

    View full-size slide

  8. KotlinͱSwiftͷasync/awaitڞ௨఺
    • Kotlinͷasync/await΍suspend͸ඇಉظؔ਺ʹ
    ΩʔϫʔυΛ෇͚Δ
    • Kotlinͷsuspend͕ಛʹSwiftͷʹࣅ͍ͯΔ
    • ͔͠͠ඍົͳҧ͍͕͋Δ

    View full-size slide

  9. ͦ΋ͦ΋async/awaitͬͯ
    ԿΑʁ

    View full-size slide

  10. async/await͸
    ඇಉظॲཧΛಉظॲཧతʹ
    ίʔσΟϯάͰ͖Δ
    ඇಉظॲཧΛ଴ͭ
    ίʔϧόοΫ஍ࠈʹͳΒͳͯ͘ࡁΉͶʂ

    View full-size slide

  11. 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
    }

    View full-size slide

  12. 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Λ͚ͭΔ

    View full-size slide

  13. 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Ͱ݁ՌΛ଴ͬͯऔΓग़͢Α͏ʹ
    هड़Ͱ͖·͢

    View full-size slide

  14. Kotlin 1.2.51ͷasync/await
    BTZODBXBJU͸·ͩFYQFSJNFOUBM͔ͩΒ
    ͙͢มߋ͞ΕΔ͔΋͠Εͳ͍Α

    View full-size slide

  15. Kotlin async/awaitͰಉ͡ྫΛهड़ͯ͠ΈΔ
    fun loadWebResource(resource: String): Deferred = async {
    // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ
    // @asyncͰDeferredίϯςφʹ͘Δ·ΕΔʁ
    return@async Resource()
    }
    fun processImageData1(): Deferred = 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
    }

    View full-size slide

  16. Kotlin async/awaitͰಉ͡ྫΛهड़ͯ͠ΈΔ
    fun loadWebResource(resource: String): Deferred = async {
    // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ
    // @asyncͰDeferredίϯςφʹ͘Δ·ΕΔʁ
    return@async Resource()
    }
    fun processImageData1(): Deferred = 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Λฦ͢

    View full-size slide

  17. Kotlin async/awaitͰಉ͡ྫΛهड़ͯ͠ΈΔ
    fun loadWebResource(resource: String): Deferred = async {
    // ͦͷ··ฦ͚͢Ͳຊ౰͸ඇಉظॲཧ
    // @asyncͰDeferredίϯςφʹ͘Δ·ΕΔʁ
    return@async Resource()
    }
    fun processImageData1(): Deferred = 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Λฦ͢

    View full-size slide

  18. 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
    }

    View full-size slide

  19. 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Ͱͳ͘஋Λฦ͢

    View full-size slide

  20. 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͢Δඞཁ͕ͳ͘ͳΔ

    View full-size slide

  21. ͔͠͠…
    ͓෼͔Γ͍͚ͨͩͨͩΖ͏͔…
    ͜ͷྫͷsuspend͸
    ແବʹ଴ͬͯ͠·͍ͬͯΔ

    View full-size slide

  22. ͜ͷॲཧͷແବͳͱ͜Ζ͸ʁ
    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
    }

    View full-size slide

  23. ͜ͷॲཧͷແବͳͱ͜Ζ͸ʁ
    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͸ؔ࿈͠ͳ͍ඇಉظॲཧͳͷͰ
    ͭಉ࣌ʹ࣮ߦͯ݁͠ՌΛ଴ͯ͹ޮ཰͍͍ͷͰ͸ʁ

    View full-size slide

  24. 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
    }

    View full-size slide

  25. 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ʹ

    View full-size slide

  26. 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
    Ͱ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  27. 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ͳϝιουͰ
    Ͱ͘ΔΉ΄͏͕සൟʹ͔ͭ͏ͩΖ͏ͱ͸ࢥ͍·͢ɻ

    View full-size slide

  28. 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
    }

    View full-size slide

  29. 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Ͱ͘ΔΉ

    View full-size slide

  30. 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ϝιουͰ݁ՌΛ଴ͭ

    View full-size slide

  31. 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ʹ͍ۙײ৮

    View full-size slide

  32. ͞Βʹ஌Γ͍ͨਓͷͨΊʹ

    View full-size slide

  33. Swift 5 async/await ݚڀಡຊ
    • ϥΠϒϥϦHydraͰSwift 4Ͱ΋async/awaitΛ࣮ݱ͢Δ
    • Hydra͕ͲͷΑ͏ʹͯ͠εϨουΛࢭΊ͍ͯΔ͔ղઆ
    • async/await ϓϩϙʔβϧυϥϑτͷղઆ
    • JS, C#, Dart, KotlinͰͷasync/awaitղઆ
    IUUQTCPPUIQNKBJUFNT

    View full-size slide