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.

Subhrajyoti Sen

September 22, 2019
Tweet

More Decks by Subhrajyoti Sen

Other Decks in Programming

Transcript

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

    View Slide

  2. The Main Thread

    View Slide

  3. The Main Thread

    Default thread that apps start on

    View Slide

  4. The Main Thread

    Default thread that apps start on

    Known as the UI thread

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  8. The Main Thread is Precious

    View Slide

  9. The Main Thread is Precious

    Refreshed at a regular interval

    View Slide

  10. The Main Thread is Precious

    Refreshed at a regular interval

    Should be never blocked

    View Slide

  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

    View Slide

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

    View Slide

  13. View Slide

  14. The Solution

    View Slide

  15. The Solution
    Do work in the background

    View Slide

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

    View Slide

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

    View Slide

  18. Why is background work challenging?

    View Slide

  19. A process is not forever
    - Keith Smith

    View Slide

  20. The Process Priority Table

    View Slide

  21. The Process Priority Table
    1. Native

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  30. Solutions by Android

    View Slide

  31. Solutions by Android
    AsyncTask
    Coroutines
    RxJava
    AlarmManager
    Foreground Service
    Loader
    Thread and Handler
    Intent Service
    WorkManager Job Scheduler

    View Slide

  32. So many solutions!!!

    View Slide

  33. BUT WHY

    View Slide

  34. Changes Introduced in the OS

    View Slide

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

    View Slide

  36. Changes Introduced in the OS

    Android 7.0 introduced Doze on the Go

    View Slide

  37. Changes Introduced in the OS

    Android 7.0 introduced Doze on the Go

    Android 8.0 introduced

    Background Service Limitations

    Broadcast Limitations

    View Slide

  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

    View Slide

  39. App Standby Buckets

    View Slide

  40. App Standby Buckets

    Use machine learning to determine how likely each app will be
    used.

    View Slide

  41. App Standby Buckets

    Use machine learning to determine how likely each app will be
    used.

    Places app in one of 5 priority buckets

    View Slide

  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

    View Slide

  43. The “Not Recommended” Solutions

    View Slide

  44. AsyncTask

    View Slide

  45. AsyncTask

    Does not auto-cancel when Activity/Fragment is
    destroyed/recreated

    View Slide

  46. AsyncTask

    Does not auto-cancel when Activity/Fragment is
    destroyed/recreated

    Can lead to memory leaks

    View Slide

  47. AsyncTask

    Does not auto-cancel when Activity/Fragment is
    destroyed/recreated

    Can lead to memory leaks

    Not really easy to cancel

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  51. Quiz 1:
    What feature of Android Pie did I mention that
    intelligently prioritizes apps?

    View Slide

  52. Quiz 2:
    Name any 3 buckets from the App Standy
    Buckets

    View Slide

  53. Loaders

    View Slide

  54. Loaders

    Deprecated on Android P (API 28)

    View Slide

  55. Loaders

    Deprecated on Android P (API 28)

    Confusing API

    View Slide

  56. Loaders

    Deprecated on Android P (API 28)

    Confusing API

    Can be unpredictable

    View Slide

  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

    View Slide

  58. The “Recommended” Solutions

    View Slide

  59. DownloadManager

    View Slide

  60. DownloadManager

    Perform long-running HTTP downloads

    View Slide

  61. DownloadManager

    Perform long-running HTTP downloads

    Handles HTTP interaction

    View Slide

  62. DownloadManager

    Perform long-running HTTP downloads

    Handles HTTP interaction

    Retry on failure

    View Slide

  63. DownloadManager

    Perform long-running HTTP downloads

    Handles HTTP interaction

    Retry on failure

    Continuation after reboot

    View Slide

  64. DownloadManager

    Perform long-running HTTP downloads

    Handles HTTP interaction

    Retry on failure

    Continuation after reboot

    Handles connectivity changes

    View Slide

  65. DownloadManager

    Perform long-running HTTP downloads

    Handles HTTP interaction

    Retry on failure

    Continuation after reboot

    Handles connectivity changes

    Notifies user of progress updates

    View Slide

  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

    View Slide

  67. Quiz 3:
    Which API level was Loader deprecated in?

    View Slide

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

    View Slide

  69. Foreground Services

    View Slide

  70. Foreground Services

    High priority is the process ranking system.

    View Slide

  71. Foreground Services

    High priority is the process ranking system.

    Non-dismissible notification

    View Slide

  72. Foreground Services

    High priority is the process ranking system.

    Non-dismissible notification

    Should be used for immediate, important tasks

    View Slide

  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

    View Slide

  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)

    View Slide

  75. Quiz 5:
    What would be the priority rank of Foreground
    Service in the Process Priority Table?

    View Slide

  76. AlarmManager

    View Slide

  77. AlarmManager

    Schedule tasks at a particular time

    View Slide

  78. AlarmManager

    Schedule tasks at a particular time

    Schedule tasks to execute at intervals

    View Slide

  79. AlarmManager

    Schedule tasks at a particular time

    Schedule tasks to execute at intervals

    Available since API 1

    View Slide

  80. AlarmManager

    Schedule tasks at a particular time

    Schedule tasks to execute at intervals

    Available since API 1
    Gotchas:

    View Slide

  81. AlarmManager

    Schedule tasks at a particular time

    Schedule tasks to execute at intervals

    Available since API 1
    Gotchas:

    Alarms reset after phone reboot

    View Slide

  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

    View Slide

  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

    View Slide

  84. Quiz 6:
    Whose blog post did I mention for AsyncTask?

    View Slide

  85. JobScheduler

    View Slide

  86. JobScheduler

    Available only on API 23+

    View Slide

  87. JobScheduler

    Available only on API 23+

    Framework intelligently schedules and batches tasks

    View Slide

  88. JobScheduler

    Available only on API 23+

    Framework intelligently schedules and batches tasks

    You can specify conditions for execution

    View Slide

  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

    View Slide

  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

    View Slide

  91. WorkManager

    View Slide

  92. WorkManager

    Provides guarantee that job will be executed

    View Slide

  93. WorkManager

    Provides guarantee that job will be executed

    Backward compatible till API 14

    View Slide

  94. WorkManager

    Provides guarantee that job will be executed

    Backward compatible till API 14

    Survives app or phone restart

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  98. Guidelines

    View Slide

  99. Guidelines
    1. Is the work immediate?

    View Slide

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

    View Slide

  101. Guidelines
    1. Is the work immediate?
    2. Does it depend on system conditions?
    3. Does it have to happen at a specific time?

    View Slide

  102. Quiz 7:
    What is the title of this talk?

    View Slide

  103. Thank You...
    @iamsubhrajyoti @SubhrajyotiSen

    View Slide