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

実践 WorkManager / WorkManager

D62ad26d5db87a3a62ae97c77a252c22?s=47 atsushisakai
February 08, 2019

実践 WorkManager / WorkManager

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

D62ad26d5db87a3a62ae97c77a252c22?s=128

atsushisakai

February 08, 2019
Tweet

Transcript

  1. ࣮ફ WorkManager ञҪ ಞ @_atsushisakai mixi, Inc. DroidKaigi 2019 Feb.

    8 17:40~ Room 1
  2. ञҪ ಞ @_atsushisakai 2011೥ mixi, Inc.ೖࣾ 2014೥ Ո଒ΞϧόϜʮΈͯͶʯͷ։ൃ։࢝ ʢiOS൛ͷ୲౰ʣ 2018೥

    ΈͯͶࣄۀ෦։ൃάϧʔϓϚωʔδϟʔ ݉ SRE
  3. ͜ͷൃදͰ͓࿩͍ͨ͜͠͠ͱɾͶΒ͍ • ղܾ͢΂͖՝୊͸ͳΜͩͬͨͷ͔ʁ • ͳͥWorkManagerΛબ୒ͨ͠ͷ͔ʁ • ͦͷ੒Ռ͸ʁ • ࣄྫΛ͓఻͑͢Δ͜ͱͰɺ՝୊ղܾͷબ୒ࢶΛ૿΍ͯ͠΋Β͍͍ͨɻ

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

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

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

  7. ՝୊ 2

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

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

  10. ՝୊ 2

  11. Response Time 0.71 sec. Response Time 0.17 sec.

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

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

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

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

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

  18. େࡶ೺ͳཁٻ 1. ΞϓϦ͕ىಈ͍ͯ͠ͳ͍࣌ʹॲཧΛߦ͏͜ͱɻ 2. Wi-Fi઀ଓ͍ͯ͠Δ࣌ͷΈॲཧΛߦ͏͜ͱɻ 3. όοςϦʔ͕ॆ෼ʹ͋Δͱ͖ʹॲཧΛߦ͏͜ͱɻ 4. ৽͍͠ΩϟογϡΛऔಘ͢ΔͨΊʹ1೔ʹ1ճ͘Β͍͸ॲཧΛߦ͏͜ͱɻ 5.

    minSdkVersion 19Ͱ΋ಈ͘ϓϩάϥϜͰ͋Δ͜ͱɻ WorkManager Λ࠾༻͢Δ·ͰͷܦҢ 3
  19. WorkManagerͱ͸ʁ • όοΫάϥ΢ϯυδϣϒΛ࣮ߦɾ؅ཧ͢ΔͨΊͷAndroid Jetpackίϯϙʔωϯτ • ΞϓϦ͕ऴྃͯ͠΋࣮֬ʹλεΫΛऴ͍͑ͨॲཧΛ࣮ߦͤ͞Δ • ༷ʑͳ੍໿ʢConstraintsʣΛ૊Έ߹Θͤͯδϣϒͷ࣮ߦΛ੍ޚ͢Δ͜ͱ͕Ͱ͖Δ • API

    14Ҏ߱Ͱಉ͡ίʔυ͕ಈ͘ • API 23+Ͱ͸JobScheduler͕࢖ΘΕΔ • API 14~22Ͱ͸AlarmManager + BroadcastReceiver͕࢖ΘΕΔ WorkManager Λ࠾༻͢Δ·ͰͷܦҢ 3
  20. WorkManager͸ը૾Ωϟογϡʹ࠷ద WorkManager Λ࠾༻͢Δ·ͰͷܦҢ 3

  21. ࣮૷ 4

  22. ࣮૷ 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
  23. ࣮૷ 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
  24. Enqueue Job ࣮૷ 4

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

  26. ࣮૷ 4

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

  28. ࣮૷ 4

  29. ࣮૷ 4

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

  31. ࣮૷ 4

  32. ࣮૷ 4

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

  34. Execute Job ࣮૷ 4

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

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

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

  38. ϦτϥΠͷڍಈΛཧղ͢Δ • σϑΥϧτ͸BackoffPolicy.EXPONENTIAL • WorkSpec#calculateNextRunTime ΛಡΉͱΞϧΰϦζϜΛཧղͰ͖Δɻ • ࠷ऴతʹ਺೔ޙʹJob͕ϦτϥΠ͞ΕΔ͜ͱ΋͋ΓಘΔɻ • ϦτϥΠճ਺ͷ࠷େ஋͸ઃఆͰ͖ͳ͍ɻ

    • Worker#getRunAttemptCount Ͱݱࡏͷࢼߦճ਺Λऔಘ͠ɺFailureͰऴ͑Δɻ • ͦ΋ͦ΋ҙਤ͠ͳ͍ϦτϥΠ͕ߦΘΕͳ͍Α͏ʹΤϥʔϋϯυϦϯά͓ͯ͘͠ࣄ΋ॏཁɻ ࣮૷࣌ͷTIPS΍஫ҙࣄ߲ 5
  39. WorkerͷίʔυΛΞοϓσʔτ͢Δͱ͖ʹ͸஫ҙ • InputDataͷܕ΍ΩʔΛແҋʹมߋͯ͠͠·͏ͱɺલͷόʔδϣϯͰenqueueͨ͠Job͕Ξοϓσʔτޙ ʹ࣮ߦ͞ΕͯɺWorkerͷ࣮૷͕มΘͬͯҙਤ͠ͳ͍ΤϥʔʹͳΔɻ • WorkerͷΫϥε໊Λมߋͨ͠৔߹͸ɺ࣮ߦ࣌ʹinitͰ͖ͣɺ͙͢ʹFailureѻ͍ʹͳΔɻ ࣮૷࣌ͷTIPS΍஫ҙࣄ߲ 5

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

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

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

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

  44. ςετʹ͍ͭͯ 6

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

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

  47. ςετʹ͍ͭͯ 6

  48. • https://issuetracker.google.com/issues/122553577 > We never supported Roboelectric. All WorkManager tests

    need to run as an Android 
 > instrumentation test. ςετʹ͍ͭͯ 6
  49. ੒Ռ 7

  50. Response Time 0.71 sec. Get from cache

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

  52. ࢒͞Εͨ՝୊ 8

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

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

  55. ·ͱΊ 9

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

  57. THANK YOU!!