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

実践 WorkManager / WorkManager

atsushisakai
February 08, 2019

実践 WorkManager / WorkManager

DroidKaigi 2019の登壇資料です。
こちらの詳細はブログエントリにもまとめていますので合わせてご覧ください。
https://medium.com/mixi-developers/%E5%AE%9F%E8%B7%B5-workmanager-ea9344bf09a3

atsushisakai

February 08, 2019
Tweet

More Decks by atsushisakai

Other Decks in Programming

Transcript

  1. ࣮ફ WorkManager
    ञҪ ಞ @_atsushisakai
    mixi, Inc.
    DroidKaigi 2019 Feb. 8 17:40~ Room 1

    View full-size slide

  2. ञҪ ಞ @_atsushisakai
    2011೥ mixi, Inc.ೖࣾ
    2014೥ Ո଒ΞϧόϜʮΈͯͶʯͷ։ൃ։࢝ ʢiOS൛ͷ୲౰ʣ
    2018೥ ΈͯͶࣄۀ෦։ൃάϧʔϓϚωʔδϟʔ ݉ SRE

    View full-size slide

  3. ͜ͷൃදͰ͓࿩͍ͨ͜͠͠ͱɾͶΒ͍
    • ղܾ͢΂͖՝୊͸ͳΜͩͬͨͷ͔ʁ
    • ͳͥWorkManagerΛબ୒ͨ͠ͷ͔ʁ
    • ͦͷ੒Ռ͸ʁ
    • ࣄྫΛ͓఻͑͢Δ͜ͱͰɺ՝୊ղܾͷબ୒ࢶΛ૿΍ͯ͠΋Β͍͍ͨɻ

    View full-size slide

  4. 1 ʮΈͯͶʯʹ͍ͭͯ

    View full-size slide

  5. ʮΈͯͶʯʹ͍ͭͯ
    ಛ௃
    ࢠڙͷࣸਅ΍ಈըΛڞ༗ɺ੔ཧ
    Ո଒͚ͩͰ҆৺ͯ͠࢖͑ΔΞϧόϜΞϓϦ
    2019೥1݄࣌఺ͰϢʔβʔ਺400ສਓ
    1

    View full-size slide

  6. ʮΈͯͶʯʹ͍ͭͯ
    2017೥7݄ ӳޠ൛ͷఏڙ։࢝
    1

    View full-size slide

  7. ՝୊
    ࣸਅͷද͕ࣔ஗͍
    • ւ֎ͷϢʔβʔ͔Βͷ໰͍߹ΘͤɾϨϏϡʔ͕ଟ͔ͬͨɻ
    • ࣌ંɺࠃ಺ʹ͓͍ͯ΋੠͸্͕͍ͬͯͨɻ
    2

    View full-size slide

  8. ՝୊
    ෼ੳ: Ͳͷ͙Β͍஗͍ͷ͔ʁ
    2

    View full-size slide

  9. Response Time 0.71 sec. Response Time 0.17 sec.

    View full-size slide

  10. ݱ࣮: New York MidtownͷWeWork Wi-FiͰίϨ

    View full-size slide

  11. ՝୊
    ղܾ͍ͨ͜͠ͱ
    • ฏۉ஋0.54ඵͷࠩΛຒΊΔͨΊͷUXվળɻ
    • ੈքதͷͲͷࠃͰ΋೔ຊͱಉ͡ʢ0.17ඵҎԼʣମݧΛͯ͠΋Β͍͍ͨɻ
    2

    View full-size slide

  12. WorkManagerΛ࠾༻͢Δ·ͰͷܦҢ
    3

    View full-size slide

  13. CDNʹͳΔૣͰΩϟογϡͤ͞Ε͹ྑ͍ͷͰ͸ʁ
    WorkManagerΛ࠾༻͢Δ·ͰͷܦҢ
    3

    View full-size slide

  14. AndroidΞϓϦͷىಈલʹը૾ΛΩϟογϡ͓͚ͯ͠͹ྑ͍ͷͰ͸ʁ
    WorkManager Λ࠾༻͢Δ·ͰͷܦҢ
    3

    View full-size slide

  15. େࡶ೺ͳཁٻ
    1. ΞϓϦ͕ىಈ͍ͯ͠ͳ͍࣌ʹॲཧΛߦ͏͜ͱɻ
    2. Wi-Fi઀ଓ͍ͯ͠Δ࣌ͷΈॲཧΛߦ͏͜ͱɻ
    3. όοςϦʔ͕ॆ෼ʹ͋Δͱ͖ʹॲཧΛߦ͏͜ͱɻ
    4. ৽͍͠ΩϟογϡΛऔಘ͢ΔͨΊʹ1೔ʹ1ճ͘Β͍͸ॲཧΛߦ͏͜ͱɻ
    5. minSdkVersion 19Ͱ΋ಈ͘ϓϩάϥϜͰ͋Δ͜ͱɻ
    WorkManager Λ࠾༻͢Δ·ͰͷܦҢ
    3

    View full-size slide

  16. WorkManagerͱ͸ʁ
    • όοΫάϥ΢ϯυδϣϒΛ࣮ߦɾ؅ཧ͢ΔͨΊͷAndroid Jetpackίϯϙʔωϯτ
    • ΞϓϦ͕ऴྃͯ͠΋࣮֬ʹλεΫΛऴ͍͑ͨॲཧΛ࣮ߦͤ͞Δ
    • ༷ʑͳ੍໿ʢConstraintsʣΛ૊Έ߹Θͤͯδϣϒͷ࣮ߦΛ੍ޚ͢Δ͜ͱ͕Ͱ͖Δ
    • API 14Ҏ߱Ͱಉ͡ίʔυ͕ಈ͘
    • API 23+Ͱ͸JobScheduler͕࢖ΘΕΔ
    • API 14~22Ͱ͸AlarmManager + BroadcastReceiver͕࢖ΘΕΔ
    WorkManager Λ࠾༻͢Δ·ͰͷܦҢ
    3

    View full-size slide

  17. WorkManager͸ը૾Ωϟογϡʹ࠷ద
    WorkManager Λ࠾༻͢Δ·ͰͷܦҢ
    3

    View full-size slide

  18. ࣮૷
    4
    Synchronizer
    Server/CDN SQLite WorkManager Worker Glide
    Synchronizer
    Server/CDN SQLite WorkManager Worker Glide
    Request Metadata
    Response Metadata
    Save Metadata
    Enqueue Job
    Cache Image
    Select Metadata
    Request Image
    Response Image
    Execute Job

    View full-size slide

  19. ࣮૷
    4
    Synchronizer
    Server/CDN SQLite WorkManager Worker Glide
    Synchronizer
    Server/CDN SQLite WorkManager Worker Glide
    Request Metadata
    Response Metadata
    Save Metadata
    Enqueue Job
    Cache Image
    Select Metadata
    Request Image
    Response Image
    Execute Job

    View full-size slide

  20. Enqueue Job
    ࣮૷
    4

    View full-size slide

  21. Constraints ෇͖ͷ Enqueue
    • Wi-FiωοτϫʔΫʹ઀ଓ͍ͯ͠Δ
    • όοςϦʔ͕ॆ෼ʹ͋Δɺ΋͘͠͸ॆిதͰ͋Δ
    ࣮૷
    4

    View full-size slide

  22. Jobͷ܁Γฦ࣮͠ߦ
    • 24࣌ؒʹ1ճ࣮ߦ͍ͤͨ͞ɻ
    ࣮૷
    4

    View full-size slide

  23. PeriodicWorkRequest
    • ܁Γฦ͠ಉ͡JobΛIntevalຖʹ࣮ߦͤ͞ΔΑ͏WorkManagerʹϦΫΤετ͢Δɻ
    • ࠷ॳͷ࣮ߦ͸ଈ࣌΋͘͠͸੍໿Λຬͨͨ͠௚ޙʹߦΘΕΔɻ
    https://developer.android.com/reference/androidx/work/PeriodicWorkRequest
    ࣮૷
    4

    View full-size slide

  24. enqueueUniquePeriodicWork
    • ϢχʔΫͳ໊લͷ෇͍ͨPeriodicWorkRequestΛৗʹͻͱ͚ͭͩenqueue͞Ε͍ͯΔঢ়ଶΛอূͰ͖Δɻ
    • ಉ࣌ʹ;ͨͭͷJob͕ฒྻʹಈ͘ඞཁ͕ແ͍৔߹ʹ࠷దͳenqueueํ๏ɻ
    https://developer.android.com/reference/androidx/work/WorkManager#enqueueUniquePeriodicWork
    ࣮૷
    4

    View full-size slide

  25. Execute Job
    ࣮૷
    4

    View full-size slide

  26. Workerͷ࣮૷
    • ࣮૷಺༰͸୯७
    • SQLite͔ΒϨίʔυΛऔಘ
    • URLʹΞΫηε͠ը૾Λऔಘ
    • Glideʹऔಘͨ͠ը૾ΛΩϟογϡ
    ࣮૷
    4

    View full-size slide

  27. ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  28. Worker͸ႈ౳ੑΛ୲อͨ͠ॲཧΛॻ͘
    • ಉ͡ύϥϝʔλͰෳ਺ճJob͕enqueue͞Ε࣮ߦ͞Εͯ΋ৗʹಉ݁͡Ռɾঢ়ଶ͕ฦΔΑ͏ʹ࣮૷͢Δɻ
    ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  29. ϦτϥΠͷڍಈΛཧղ͢Δ
    • σϑΥϧτ͸BackoffPolicy.EXPONENTIAL
    • WorkSpec#calculateNextRunTime ΛಡΉͱΞϧΰϦζϜΛཧղͰ͖Δɻ
    • ࠷ऴతʹ਺೔ޙʹJob͕ϦτϥΠ͞ΕΔ͜ͱ΋͋ΓಘΔɻ
    • ϦτϥΠճ਺ͷ࠷େ஋͸ઃఆͰ͖ͳ͍ɻ
    • Worker#getRunAttemptCount Ͱݱࡏͷࢼߦճ਺Λऔಘ͠ɺFailureͰऴ͑Δɻ
    • ͦ΋ͦ΋ҙਤ͠ͳ͍ϦτϥΠ͕ߦΘΕͳ͍Α͏ʹΤϥʔϋϯυϦϯά͓ͯ͘͠ࣄ΋ॏཁɻ
    ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  30. WorkerͷίʔυΛΞοϓσʔτ͢Δͱ͖ʹ͸஫ҙ
    • InputDataͷܕ΍ΩʔΛແҋʹมߋͯ͠͠·͏ͱɺલͷόʔδϣϯͰenqueueͨ͠Job͕Ξοϓσʔτޙ
    ʹ࣮ߦ͞ΕͯɺWorkerͷ࣮૷͕มΘͬͯҙਤ͠ͳ͍ΤϥʔʹͳΔɻ
    • WorkerͷΫϥε໊Λมߋͨ͠৔߹͸ɺ࣮ߦ࣌ʹinitͰ͖ͣɺ͙͢ʹFailureѻ͍ʹͳΔɻ
    ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  31. Worker͸λΠϜΞ΢τ͞ΕΔ
    • Jobຖʹ10෼ؒͷλΠϜΞ΢τ஋͕ଘࡏ͍ͯ͠Δɻ
    • ௕͘ͳΓͦ͏ͳॲཧΛ࣮ߦ͢Δͱ͖͸ɺॲཧͷ୯ҐΛࡉ෼Խ͠ෳ਺ͷWorkerΛνΣʔϯ͢ΔΑ͏
    ʹߏ੒͢Δɻʢ࣍ϖʔδࢀরʣ
    ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  32. ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5
    https://developer.android.com/reference/androidx/work/WorkManager

    View full-size slide

  33. WorkerΛcancel͢Δ࣌ͷڍಈʹ஫ҙ
    • WorkManagerʹWorkerΛcancel͢ΔΠϯλʔϑΣʔε͕͋Δ͕શͯϕετΤϑΥʔτͰ͋Δɻ
    • ಈ͍͍ͯΔWorker͕͙͢͞·ఀࢭ͞ΕΔΘ͚Ͱ͸ͳ͍ɻ
    • ಈ͍͍ͯΔWorker͕cancelཁٻ͞Εͨͱ͖ʹԿ͔Λ͍ͨ͠৔߹͸ҎԼΛ֬ೝ͢Δɻ
    • onStopped͕࣮ߦ͞ΕΔ
    • isStoppedͰtrue͕ฦΔΑ͏ʹͳΔ
    ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  34. QA͕೉͍͠
    • όοΫάϥ΢ϯυॲཧ͸ݟ͑ͳ͍ͷͰσόοάը໘Λ༻ҙ͢ΔͳͲͯ͠޻෉͢Δɻ
    • ඞཁʹԠͯ͡WorkInfoΛऔಘͯ͠Jobͷঢ়ଶΛݟ͑ΔΑ͏ʹ͓ͯ͘͠ɻ
    ࣮૷࣌ͷTIPS΍஫ҙࣄ߲
    5

    View full-size slide

  35. ςετʹ͍ͭͯ
    6

    View full-size slide

  36. ͲͷΑ͏ʹςετΛهड़͢Δͷ͔
    • WorkerΛಉظ࣮ߦ͠ɺҰ࿈ͷTransactionͷ಺༰Λςετ͢Δɻ
    • WorkManagerTestInitHelperͱ͍͏Ϋϥε͕༻ҙ͞Ε͍ͯΔͷͰ͜ΕΛར༻͢Δɻ
    • ςετͷ಺༰
    • WorkInfo#outputDataΛ֬ೝͨ͠Γɺґଘ͢ΔϥΠϒϥϦͷverifyΛߦͳ͍ͬͯ͘ɻ
    ςετʹ͍ͭͯ
    6

    View full-size slide

  37. WorkManagerTestInitHelperʹ͍ͭͯ
    • Test࣮ߦ࣌ͷContextΛ࢖ͬͯγϯάϧτϯͳWorkManagerΛinitͤ͞Δɻ
    • TestDriverΛੜ੒͠ɺConstraintsʢ੍໿ʣΛِ૷͠ɺWorkerΛ͙͢ʹ࣮ߦ͢Δ͜ͱ͕Ͱ͖Δɻ
    ςετʹ͍ͭͯ
    6

    View full-size slide

  38. ςετʹ͍ͭͯ
    6

    View full-size slide

  39. • https://issuetracker.google.com/issues/122553577
    > We never supported Roboelectric. All WorkManager tests need to run as an Android 

    > instrumentation test.
    ςετʹ͍ͭͯ
    6

    View full-size slide

  40. Response Time 0.71 sec. Get from cache

    View full-size slide

  41. େ੒ޭ
    • UI্ͷϩʔυ஗Ԇ͸Ұ੾ͳ͘ͳͬͨɻ
    • ϨεϙϯελΠϜΛҙࣝ͢Δඞཁ͕ͳ͘ͳΓɺੈքதͰಉ͡ମݧΛఏڙͰ͖ΔΑ͏ʹͳͬͨɻ
    ੒Ռ
    7

    View full-size slide

  42. ࢒͞Εͨ՝୊
    8

    View full-size slide

  43. ୺຤ଆͷϦιʔεʹཔΓ͗͢
    • ୺຤ͷόοςϦʔɾετϨʔδɾωοτϫʔΫϦ
    ιʔεʹগͳ͔ΒͣෛՙΛ͔͚͍ͯΔɻ
    • όοΫάϥ΢ϯυδϣϒͷڍಈΛϢʔβʔ͕೚ҙ
    ʹௐ੔Ͱ͖ΔΑ͏ͳUIΛ༻ҙ͍ͯ͠Δɻ
    ࢒͞Εͨ՝୊
    8

    View full-size slide

  44. ϦΫΤετʹඥ෇͔ͳ͍CDN΁ͷ೚ҙΩϟογϡػೳ͕ແ͍໰୊
    • AWSଆʹڧ͘ػೳϦΫΤετΛ͍ͯ͠Δʂ
    • ͦͷػೳΛ࣋ͭAWSҎ֎ͷCDNΛ࢖͏બ୒ࢶ΋͋Δʹ͸͋Δɻ
    ࢒͞Εͨ՝୊
    8

    View full-size slide

  45. • WorkManagerΛ࢖ͬͨUXվળͷࣄྫΛ͝঺հ͠·ͨ͠ɻ
    • ࣮ફతͳഎܠΛ౿·࣮͑ͨ૷࣌ͷϙΠϯτ΍஫ҙͨ͜͠ͱɾςετͷख๏ͳͲΛ͝঺հ͠·ͨ͠ɻ
    ·ͱΊ
    9

    View full-size slide