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

Deep Dive Work Manager

Deep Dive Work Manager

MobileConf 2018

Iñaki Villar

August 25, 2018
Tweet

More Decks by Iñaki Villar

Other Decks in Technology

Transcript

  1. Hi, I’m new Android Developer, have some questions < Android

    Ninja Warriors Rockstars Experts (116) 4:00 PM
  2. Welcome!! Sure, shoot Hi, I’m new Android Developer, have some

    questions < Android Ninja Warriors Rockstars Experts (116) 4:00 PM 4:00 PM
  3. Welcome!! Sure, shoot Hi, I’m new Android Developer, have some

    questions My app has to check in the server if I have new stock prices, I don’t know.. < Android Ninja Warriors Rockstars Experts (116) 4:01 PM 4:00 PM 4:00 PM
  4. Welcome!! Sure, shoot Hi, I’m new Android Developer, have some

    questions My app has to check in the server if I have new stock prices, I don’t know.. The first rule is don’t block the main thread < Android Ninja Warriors Rockstars Experts (116) 4:01 PM 4:02 PM 4:00 PM 4:00 PM
  5. Welcome!! Sure, shoot My app has to check in the

    server if I have new stock prices, I don’t know.. The first rule is don’t block the main thread < Android Ninja Warriors Rockstars Experts (116) 4:02 PM 4:00 PM 4:01 PM
  6. Welcome!! Sure, shoot My app has to check in the

    server if I have new stock prices, I don’t know.. The first rule is don’t block the main thread You can use a Thread < Android Ninja Warriors Rockstars Experts (116) 4:02 PM 4:00 PM 4:01 PM 4:03 PM
  7. My app has to check in the server if I

    have new stock prices, I don’t know.. The first rule is don’t block the main thread You can use a Thread < Android Ninja Warriors Rockstars Experts (116) 4:02 PM 4:01 PM 4:03 PM
  8. My app has to check in the server if I

    have new stock prices, I don’t know.. The first rule is don’t block the main thread You can use a Thread Whaaaat? Are you kidding me? Are you from the past? < Android Ninja Warriors Rockstars Experts (116) 4:02 PM 4:01 PM 4:03 PM 4:03 PM
  9. The first rule is don’t block the main thread You

    can use a Thread Whaaaat? Are you kidding me? Are you from the past? < Android Ninja Warriors Rockstars Experts (116) 4:02 PM 4:03 PM 4:03 PM
  10. The first rule is don’t block the main thread You

    can use a Thread Whaaaat? Are you kidding me? Are you from the past? You can use AsyncTask better than a Thread < Android Ninja Warriors Rockstars Experts (116) 4:02 PM 4:03 PM 4:03 PM 4:04 PM
  11. You can use a Thread Whaaaat? Are you kidding me?

    Are you from the past? You can use AsyncTask better than a Thread < Android Ninja Warriors Rockstars Experts (116) 4:03 PM 4:03 PM 4:04 PM
  12. You can use a Thread Whaaaat? Are you kidding me?

    Are you from the past? You can use AsyncTask better than a Thread 555555555 < Android Ninja Warriors Rockstars Experts (116) 4:03 PM 4:03 PM 4:04 PM 4:05 PM
  13. Whaaaat? Are you kidding me? Are you from the past?

    You can use AsyncTask better than a Thread 555555555 < Android Ninja Warriors Rockstars Experts (116) 4:03 PM 4:04 PM 4:05 PM GUYS, are you Android Developers? You know what is a service? 4:05 PM
  14. You can use AsyncTask better than a Thread 555555555 <

    Android Ninja Warriors Rockstars Experts (116) 4:04 PM 4:05 PM GUYS, are you Android Developers? You know what is a service? 4:05 PM
  15. You can use AsyncTask better than a Thread 555555555 <

    Android Ninja Warriors Rockstars Experts (116) 4:04 PM 4:05 PM GUYS, are you Android Developers? You know what is a service? 4:05 PM Service? 4:06 PM
  16. 555555555 < Android Ninja Warriors Rockstars Experts (116) 4:05 PM

    GUYS, are you Android Developers? You know what is a service? 4:05 PM Service? 4:06 PM
  17. 555555555 < Android Ninja Warriors Rockstars Experts (116) 4:05 PM

    GUYS, are you Android Developers? You know what is a service? 4:05 PM Service? 4:06 PM No, Intent Service 4:07 PM
  18. < Android Ninja Warriors Rockstars Experts (116) GUYS, are you

    Android Developers? You know what is a service? 4:05 PM Service? 4:06 PM No, Intent Service 4:07 PM
  19. < Android Ninja Warriors Rockstars Experts (116) GUYS, are you

    Android Developers? You know what is a service? 4:05 PM Service? 4:06 PM No, Intent Service 4:07 PM What? 4:07 PM
  20. < Android Ninja Warriors Rockstars Experts (116) Service? 4:06 PM

    No, Intent Service 4:07 PM What? 4:07 PM I have more than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM
  21. < Android Ninja Warriors Rockstars Experts (116) No, Intent Service

    4:07 PM What? 4:07 PM I have more than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM
  22. < Android Ninja Warriors Rockstars Experts (116) No, Intent Service

    4:07 PM What? 4:07 PM I have more than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM Do you think the poor guy is asking for ThreadPool executors 4:09 PM
  23. < Android Ninja Warriors Rockstars Experts (116) What? 4:07 PM

    I have more than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM Do you think the poor guy is asking for ThreadPool executors 4:09 PM
  24. < Android Ninja Warriors Rockstars Experts (116) What? 4:07 PM

    I have more than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM Do you think the poor guy is asking for ThreadPool executors 4:09 PM Google is providing JobSchedulers, more easy 4:10 PM
  25. < Android Ninja Warriors Rockstars Experts (116) I have more

    than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM Do you think the poor guy is asking for ThreadPool executors 4:09 PM Google is providing JobSchedulers, more easy 4:10 PM
  26. < Android Ninja Warriors Rockstars Experts (116) I have more

    than 20 years of experience and always working with ThreadPoolExecutors 4:08 PM Do you think the poor guy is asking for ThreadPool executors 4:09 PM Google is providing JobSchedulers, more easy 4:10 PM JobSchedulers? 4:11 PM
  27. < Android Ninja Warriors Rockstars Experts (116) Do you think

    the poor guy is asking for ThreadPool executors 4:09 PM Google is providing JobSchedulers, more easy 4:10 PM JobSchedulers? 4:11 PM
  28. < Android Ninja Warriors Rockstars Experts (116) Do you think

    the poor guy is asking for ThreadPool executors 4:09 PM Google is providing JobSchedulers, more easy 4:10 PM JobSchedulers? 4:11 PM RX!!! 4:12 PM
  29. < Android Ninja Warriors Rockstars Experts (116) Google is providing

    JobSchedulers, more easy 4:10 PM JobSchedulers? 4:11 PM RX!!! 4:12 PM
  30. < Android Ninja Warriors Rockstars Experts (116) Google is providing

    JobSchedulers, more easy 4:10 PM JobSchedulers? 4:11 PM RX!!! 4:12 PM RX!!! 4:12 PM
  31. < Android Ninja Warriors Rockstars Experts (116) RX!!! 4:12 PM

    RX!!! 4:12 PM RX!!! 4:13 PM Rx? 4:13 PM
  32. < Android Ninja Warriors Rockstars Experts (116) RX!!! 4:12 PM

    RX!!! 4:13 PM Rx? 4:13 PM Don’t worry, these kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM
  33. < Android Ninja Warriors Rockstars Experts (116) RX!!! 4:13 PM

    Rx? 4:13 PM Don’t worry, these kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM
  34. < Android Ninja Warriors Rockstars Experts (116) RX!!! 4:13 PM

    Rx? 4:13 PM Don’t worry, these kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM Ohh yes, you can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM
  35. < Android Ninja Warriors Rockstars Experts (116) Rx? 4:13 PM

    Don’t worry, these kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM Ohh yes, you can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM
  36. < Android Ninja Warriors Rockstars Experts (116) Rx? 4:13 PM

    Don’t worry, these kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM Ohh yes, you can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM 4:16 PM
  37. < Android Ninja Warriors Rockstars Experts (116) Don’t worry, these

    kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM Ohh yes, you can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM 4:16 PM
  38. < Android Ninja Warriors Rockstars Experts (116) Don’t worry, these

    kids likes lot of Rx Java but then they don’t know what is a Looper and Handler 4:14 PM Ohh yes, you can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM 4:16 PM 4:17 PM
  39. < Android Ninja Warriors Rockstars Experts (116) Ohh yes, you

    can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM 4:16 PM 4:17 PM
  40. < Android Ninja Warriors Rockstars Experts (116) Ohh yes, you

    can still use Windows 95 and Cobol with Thread pool Executors 4:15 PM 4:16 PM 4:17 PM Sorry but still I don’t know how to execute a Background task 4:18 PM
  41. < Android Ninja Warriors Rockstars Experts (116) 4:16 PM 4:17

    PM Sorry but still I don’t know how to execute a Background task 4:18 PM
  42. < Android Ninja Warriors Rockstars Experts (116) 4:16 PM 4:17

    PM Sorry but still I don’t know how to execute a Background task 4:18 PM WHY YOU DON’T SEARCH IN GOOGLE??? 4:19 PM
  43. < Android Ninja Warriors Rockstars Experts (116) 4:17 PM Sorry

    but still I don’t know how to execute a Background task 4:18 PM WHY YOU DON’T SEARCH IN GOOGLE??? 4:19 PM
  44. < Android Ninja Warriors Rockstars Experts (116) 4:17 PM Sorry

    but still I don’t know how to execute a Background task 4:18 PM WHY YOU DON’T SEARCH IN GOOGLE??? 4:19 PM But… 4:20 PM
  45. < Android Ninja Warriors Rockstars Experts (116) Sorry but still

    I don’t know how to execute a Background task 4:18 PM WHY YOU DON’T SEARCH IN GOOGLE??? 4:19 PM But… 4:20 PM
  46. < Android Ninja Warriors Rockstars Experts (116) Sorry but still

    I don’t know how to execute a Background task 4:18 PM WHY YOU DON’T SEARCH IN GOOGLE??? 4:19 PM But… 4:20 PM You have been forced to leave the group 4:21PM
  47. < Android Ninja Warriors Rockstars Experts (116) WHY YOU DON’T

    SEARCH IN GOOGLE??? 4:19 PM But… 4:20 PM You have been forced to leave the group 4:21PM
  48. < Android Ninja Warriors Rockstars Experts (116) WHY YOU DON’T

    SEARCH IN GOOGLE??? 4:19 PM But… 4:20 PM You have been forced to leave the group 4:21PM I’m late for the party? You can use coroutines
  49. Job Scheduler Service Firebase Job Dispatcher #mobconf18 Doze Mode App

    Standby Background Restricted Apps Cached Wakelocks
  50. Job Scheduler Service Firebase Job Dispatcher #mobconf18 Doze Mode App

    Standby Background Restricted Apps Cached Wakelocks
  51. Best Effort Guaranteed Execution Exact Timing Deferrable Thread Pool Foreground

    Service Thread Pool JobScheduler JobDispatcher AlarmManager BroadcastReceivers #mobconf18
  52. Best Effort Guaranteed Execution Exact Timing Deferrable Thread Pool Foreground

    Service Thread Pool JobScheduler JobDispatcher AlarmManager BroadcastReceivers #mobconf18
  53. WORK API >= 23 ? JOBSCHEDULER EXECUTOR Play Services? FIREBASE

    JOBDISPATCHER ALARM MANAGER + BROADCAST No No Yes Yes #mobconf18
  54. class ProcessData : Worker() { override fun doWork(): Result {

    myLongTaskProcess() return Result.SUCCESS } } #mobconf18
  55. class ProcessData : Worker() { override fun doWork(): Result {

    val result = myLongTaskProcess() val outputData = Data.Builder() .putInt(KEY, result) .build() setOutputData(outputData) return Result.SUCCESS } } #mobconf18
  56. class ProcessData : Worker() { override fun doWork(): Result {

    val result = myLongTaskProcess() val outputData = Data.Builder() .putInt(KEY, result) .build() setOutputData(outputData) return Result.SUCCESS } } Worker UUID Extras #mobconf18
  57. class ProcessData : Worker() { override fun doWork(): Result {

    myLongTaskProcess() return Result.SUCCESS } } #mobconf18
  58. class ProcessData : Worker() { override fun doWork(): Result {

    myLongTaskProcess() return Result.SUCCESS } } WorkRequest #mobconf18
  59. class ProcessData : Worker() { override fun doWork(): Result {

    myLongTaskProcess() return Result.SUCCESS } } OneTimeWorkRequest.Builder(ProcessData::class.java) WorkRequest #mobconf18
  60. class ProcessData : Worker() { override fun doWork(): Result {

    myLongTaskProcess() return Result.SUCCESS } } OneTimeWorkRequest.Builder(ProcessData::class.java) WorkRequest PeriodicWorkRequest.Builder(ProcessData::class.java, 15, TimeUnit.MINUTES) #mobconf18
  61. class ProcessData : Worker() { override fun doWork(): Result {

    myLongTaskProcess() return Result.SUCCESS } } OneTimeWorkRequest.Builder(ProcessData::class.java) .setConstraints(with(Constraints.Builder()) { setRequiredNetworkType(NetworkType.CONNECTED) setRequiresCharging(true) setRequiresDeviceIdle(true) build() }) #mobconf18
  62. .getStatusById(process.id) @Override public @NonNull LiveData<WorkStatus> getStatusById(@NonNull UUID id) { }

    .observe(this, Observer {}) @MainThread public void observe(LifecycleOwner owner, @NonNull Observer<T> observer) { } #mobconf18
  63. Process2 Process4 Process5 Process1 Process3 val chain1 = WorkManager.getInstance() .beginWith(process1)

    .then(process2) val chain2 = WorkManager.getInstance() .beginWith(process3) .then(process4) val chain3 = WorkContinuation .combine(chain1, chain2) .then(process5) chain3.enqueue() #mobconf18
  64. #mobconf18 Testing? private static void setupSynchronousTaskExecutor() { WorkManagerTaskExecutor.getInstance() .setTaskExecutor(new TaskExecutor()

    { @Override public void postToMainThread(Runnable runnable) { runnable.run(); } @Override public void executeOnBackgroundThread(Runnable runnable) { runnable.run(); } }); }
  65. Upload media to a server Parse data and store it

    in database Parse data and update contents of a view Process payment transaction Use as data store OK NOT OK #mobconf18