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. 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
  2. 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
  3. 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
  4. The Main Thread is Precious • Refreshed at a regular

    interval • Should be never blocked
  5. 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
  6. 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...
  7. The Solution Do work in the background When the user

    is using the app When the user is not using the app
  8. The Process Priority Table 1. Native 2. System 3. Persistent

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

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

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

    Apps 4. Foreground Apps 5. Perceptible Apps 6. Service 7. Home 8. Previous App
  12. 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
  13. Solutions by Android AsyncTask Coroutines RxJava AlarmManager Foreground Service Loader

    Thread and Handler Intent Service WorkManager Job Scheduler
  14. Changes Introduced in the OS • Android 7.0 introduced Doze

    on the Go • Android 8.0 introduced ◦ Background Service Limitations ◦ Broadcast Limitations
  15. 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
  16. App Standby Buckets • Use machine learning to determine how

    likely each app will be used. • Places app in one of 5 priority buckets
  17. 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
  18. AsyncTask • Does not auto-cancel when Activity/Fragment is destroyed/recreated •

    Can lead to memory leaks • Not really easy to cancel
  19. 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
  20. 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
  21. 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
  22. Quiz 1: What feature of Android Pie did I mention

    that intelligently prioritizes apps?
  23. Loaders • Deprecated on Android P (API 28) • Confusing

    API • Can be unpredictable Recommended way of replacing Loaders is a combination of ViewModel and LiveData
  24. DownloadManager • Perform long-running HTTP downloads • Handles HTTP interaction

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

    • Retry on failure • Continuation after reboot • Handles connectivity changes • Notifies user of progress updates
  26. 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
  27. Foreground Services • High priority is the process ranking system.

    • Non-dismissible notification • Should be used for immediate, important tasks
  28. 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
  29. 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)
  30. Quiz 5: What would be the priority rank of Foreground

    Service in the Process Priority Table?
  31. AlarmManager • Schedule tasks at a particular time • Schedule

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

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

    tasks to execute at intervals • Available since API 1 Gotchas: • Alarms reset after phone reboot
  34. 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
  35. 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
  36. JobScheduler • Available only on API 23+ • Framework intelligently

    schedules and batches tasks • You can specify conditions for execution
  37. 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
  38. 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
  39. WorkManager • Provides guarantee that job will be executed •

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

    Backward compatible till API 14 • Survives app or phone restart • Allows chaining tasks in different ways
  41. 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
  42. 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
  43. Guidelines 1. Is the work immediate? 2. Does it depend

    on system conditions? 3. Does it have to happen at a specific time?