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

Doing Background Tasks the Right Way

Doing Background Tasks the Right Way

With the large variety of options that Android provides to perform background tasks, developers can get confused on the right way to do background work for their use case. In this talk, I discuss the various ways of doing background work on Android, the challenges various OS versions introduces and the most effective way to perform tasks for various specific use cases.

53b72671be580e70c9795c7eaf35ac12?s=128

Subhrajyoti Sen

September 22, 2019
Tweet

Transcript

  1. Doing Background Tasks the Right Way Subhrajyoti Sen / @iamsubhrajyoti

    smallcase
  2. The Main Thread

  3. The Main Thread • Default thread that apps start on

  4. The Main Thread • Default thread that apps start on

    • Known as the UI thread
  5. The Main Thread • Default thread that apps start on

    • Known as the UI thread • Changes to android.view and android.widget need to happen to this thread
  6. The Main Thread • Default thread that apps start on

    • Known as the UI thread • Changes to android.view and android.widget need to happen to this thread • Responsible for dispatching touch events
  7. The Main Thread • Default thread that apps start on

    • Known as the UI thread • Changes to android.view and android.widget need to happen to this thread • Responsible for dispatching touch events • Lifecycle callbacks happen on this thread
  8. The Main Thread is Precious

  9. The Main Thread is Precious • Refreshed at a regular

    interval
  10. The Main Thread is Precious • Refreshed at a regular

    interval • Should be never blocked
  11. The Main Thread is Precious • Refreshed at a regular

    interval • Should be never blocked • Any operation longer than a few milliseconds should not take place on the Main Thread
  12. The Main Thread is Precious • Refreshed at a regular

    interval • Should be never blocked • Any operation longer than a few milliseconds should not take place on the Main Thread Or else...
  13. None
  14. The Solution

  15. The Solution Do work in the background

  16. The Solution Do work in the background When the user

    is using the app
  17. The Solution Do work in the background When the user

    is using the app When the user is not using the app
  18. Why is background work challenging?

  19. A process is not forever - Keith Smith

  20. The Process Priority Table

  21. The Process Priority Table 1. Native

  22. The Process Priority Table 1. Native 2. System

  23. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps
  24. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps 4. Foreground Apps
  25. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps 4. Foreground Apps 5. Perceptible Apps
  26. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps 4. Foreground Apps 5. Perceptible Apps 6. Service
  27. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps 4. Foreground Apps 5. Perceptible Apps 6. Service 7. Home
  28. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps 4. Foreground Apps 5. Perceptible Apps 6. Service 7. Home 8. Previous App
  29. The Process Priority Table 1. Native 2. System 3. Persistent

    Apps 4. Foreground Apps 5. Perceptible Apps 6. Service 7. Home 8. Previous App 9. Cached Apps
  30. Solutions by Android

  31. Solutions by Android AsyncTask Coroutines RxJava AlarmManager Foreground Service Loader

    Thread and Handler Intent Service WorkManager Job Scheduler
  32. So many solutions!!!

  33. BUT WHY

  34. Changes Introduced in the OS

  35. Changes Introduced in the OS Android 6.0 introduced Doze Source:

    developer.android.com
  36. Changes Introduced in the OS • Android 7.0 introduced Doze

    on the Go
  37. Changes Introduced in the OS • Android 7.0 introduced Doze

    on the Go • Android 8.0 introduced ◦ Background Service Limitations ◦ Broadcast Limitations
  38. Changes Introduced in the OS • Android 7.0 introduced Doze

    on the Go • Android 8.0 introduced ◦ Background Service Limitations ◦ Broadcast Limitations • Android 9.0 introduced App Standy Buckets
  39. App Standby Buckets

  40. App Standby Buckets • Use machine learning to determine how

    likely each app will be used.
  41. App Standby Buckets • Use machine learning to determine how

    likely each app will be used. • Places app in one of 5 priority buckets
  42. App Standby Buckets • Use machine learning to determine how

    likely each app will be used. • Places app in one of 5 priority buckets ◦ Active ◦ Working Set ◦ Frequent ◦ Rare ◦ Never
  43. The “Not Recommended” Solutions

  44. AsyncTask

  45. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated

  46. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated •

    Can lead to memory leaks
  47. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated •

    Can lead to memory leaks • Not really easy to cancel
  48. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated •

    Can lead to memory leaks • Not really easy to cancel • Can be executed only once
  49. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated •

    Can lead to memory leaks • Not really easy to cancel • Can be executed only once • Please do not use it
  50. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated •

    Can lead to memory leaks • Not really easy to cancel • Can be executed only once • Please do not use it Recommended Read: The Hidden Pitfalls of AsyncTask by Dan Lew
  51. Quiz 1: What feature of Android Pie did I mention

    that intelligently prioritizes apps?
  52. Quiz 2: Name any 3 buckets from the App Standy

    Buckets
  53. Loaders

  54. Loaders • Deprecated on Android P (API 28)

  55. Loaders • Deprecated on Android P (API 28) • Confusing

    API
  56. Loaders • Deprecated on Android P (API 28) • Confusing

    API • Can be unpredictable
  57. Loaders • Deprecated on Android P (API 28) • Confusing

    API • Can be unpredictable Recommended way of replacing Loaders is a combination of ViewModel and LiveData
  58. The “Recommended” Solutions

  59. DownloadManager

  60. DownloadManager • Perform long-running HTTP downloads

  61. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

  62. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

    • Retry on failure
  63. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

    • Retry on failure • Continuation after reboot
  64. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

    • Retry on failure • Continuation after reboot • Handles connectivity changes
  65. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

    • Retry on failure • Continuation after reboot • Handles connectivity changes • Notifies user of progress updates
  66. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

    • Retry on failure • Continuation after reboot • Handles connectivity changes • Notifies user of progress updates Use case: download a PDF report
  67. Quiz 3: Which API level was Loader deprecated in?

  68. Quiz 4: What should you use to replace Loader?

  69. Foreground Services

  70. Foreground Services • High priority is the process ranking system.

  71. Foreground Services • High priority is the process ranking system.

    • Non-dismissible notification
  72. Foreground Services • High priority is the process ranking system.

    • Non-dismissible notification • Should be used for immediate, important tasks
  73. Foreground Services • High priority is the process ranking system.

    • Non-dismissible notification • Should be used for immediate, important tasks • Should have well defined start and finish
  74. Foreground Services • High priority is the process ranking system.

    • Non-dismissible notification • Should be used for immediate, important tasks • Should have well defined start and finish Use case: Playing music, high-accuracy location tracking (WhatsApp live location sharing)
  75. Quiz 5: What would be the priority rank of Foreground

    Service in the Process Priority Table?
  76. AlarmManager

  77. AlarmManager • Schedule tasks at a particular time

  78. AlarmManager • Schedule tasks at a particular time • Schedule

    tasks to execute at intervals
  79. AlarmManager • Schedule tasks at a particular time • Schedule

    tasks to execute at intervals • Available since API 1
  80. AlarmManager • Schedule tasks at a particular time • Schedule

    tasks to execute at intervals • Available since API 1 Gotchas:
  81. AlarmManager • Schedule tasks at a particular time • Schedule

    tasks to execute at intervals • Available since API 1 Gotchas: • Alarms reset after phone reboot
  82. AlarmManager • Schedule tasks at a particular time • Schedule

    tasks to execute at intervals • Available since API 1 Gotchas: • Alarms reset after phone reboot • Alarms reset is system time is changed
  83. AlarmManager • Schedule tasks at a particular time • Schedule

    tasks to execute at intervals • Available since API 1 Gotchas: • Alarms reset after phone reboot • Alarms reset is system time is changed Use case: WhatsApp chat backup at 2AM
  84. Quiz 6: Whose blog post did I mention for AsyncTask?

  85. JobScheduler

  86. JobScheduler • Available only on API 23+

  87. JobScheduler • Available only on API 23+ • Framework intelligently

    schedules and batches tasks
  88. JobScheduler • Available only on API 23+ • Framework intelligently

    schedules and batches tasks • You can specify conditions for execution
  89. JobScheduler • Available only on API 23+ • Framework intelligently

    schedules and batches it • You can specify conditions for execution • System holds a wakelock to ensure the device is awake till job completion
  90. JobScheduler • Available only on API 23+ • Framework intelligently

    schedules and batches it • You can specify conditions for execution • System holds a wakelock to ensure the device is awake till job completion Use case: backup data when WiFi is available
  91. WorkManager

  92. WorkManager • Provides guarantee that job will be executed

  93. WorkManager • Provides guarantee that job will be executed •

    Backward compatible till API 14
  94. WorkManager • Provides guarantee that job will be executed •

    Backward compatible till API 14 • Survives app or phone restart
  95. WorkManager • Provides guarantee that job will be executed •

    Backward compatible till API 14 • Survives app or phone restart • Allows chaining tasks in different ways
  96. WorkManager • Provides guarantee that job will be executed •

    Backward compatible till API 14 • Survives app or phone restart • Allows chaining tasks in different ways • Should only be used for jobs are not immediate and can be executed at a later time
  97. WorkManager • Provides guarantee that job will be executed •

    Backward compatible till API 14 • Survives app or phone restart • Allows chaining tasks in different ways • Should only be used for jobs are not immediate and can be executed at a later time Use case: sending analytics logs to a server
  98. Guidelines

  99. Guidelines 1. Is the work immediate?

  100. Guidelines 1. Is the work immediate? 2. Does it depend

    on system conditions?
  101. Guidelines 1. Is the work immediate? 2. Does it depend

    on system conditions? 3. Does it have to happen at a specific time?
  102. Quiz 7: What is the title of this talk?

  103. Thank You... @iamsubhrajyoti @SubhrajyotiSen