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

Work, Work - WorkManager

Work, Work - WorkManager

Slided from my presentation on WorkManager at the DutchAUG IO Extended Extended meetup.

Hugo Visser

July 19, 2018
Tweet

More Decks by Hugo Visser

Other Decks in Technology

Transcript

  1. WorkManager Part of architecture components, JetPack Schedules & execute jobs

    “...intended for tasks that require a guarantee that the system will run them even if the app exits…”
  2. Current options AlarmManager → No system coordination, no constraints, throttled

    JobScheduler GCMNetworkManager FirebaseJobDispatcher
  3. Current options AlarmManager → No system coordination, no constraints, no

    retries, throttled JobScheduler → API 21+, Coordination, constraints & retries, bugs < API 23 GCMNetworkManager FirebaseJobDispatcher
  4. Current options AlarmManager → No system coordination, no constraints, no

    retries, throttled JobScheduler → API 21+, Coordination, constraints & retries, bugs < API 23 GCMNetworkManager → JobSchedulerCompat, but not quite FirebaseJobDispatcher
  5. Current options AlarmManager → No system coordination, no constraints, no

    retries, throttled JobScheduler → API 21+, Coordination, constraints & retries, bugs < API 23 GCMNetworkManager → JobSchedulerCompat, but not quite FirebaseJobDispatcher → Pluggable schedulers, default: GCMNetworkManager
  6. WorkManager Schedule jobs using constraints & retries JobScheduler on API

    23+ AlarmManager or GCMNetworkManager through FJD < API 23
  7. New features Reduce IPC → if a job is eligible

    to run immediately, no need to “schedule” it. Monitor status → LiveData providing job status Unique jobs Tags Output parameters Orchestration of multiple jobs
  8. Orchestration B runs after A D runs after C E

    runs after B and D are completed
  9. Unit of work: Worker import androidx.work.Worker class MyWorker: Worker() {

    override fun doWork(): Result { // do the hard work return Result.SUCCESS // or Result.FAILURE or Result.RETRY } }
  10. Schedule // work-runtime-ktx gives us a nicer kotlin syntax val

    request = OneTimeWorkRequestBuilder<MyWorker>() .setConstraints(Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build()) .build() // as of alpha4 getInstance() can return null! WorkManager.getInstance()!!.enqueue(request)
  11. Monitor val status = WorkManager.getInstance()!!.getStatusById(request.id) status.observe(this, Observer { // WorkStatus

    can be null if the task id is invalid or gone it?.let { Log.d(TAG, "Task status: ${it.state}") } })
  12. Next? API still in flux Asynchronous workers Try it out,

    file bugs https://issuetracker.google.com https://developer.android.com/topic/libraries/architecture/workmanager