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

いまさらWorkManager

 いまさらWorkManager

2019/1/28に開催されたPotatotips #58にて発表した資料です

Yoshihiro WADA

January 28, 2019
Tweet

More Decks by Yoshihiro WADA

Other Decks in Programming

Transcript

  1. extends Worker doWork() Worker.Result.success() / failure() Worker class MyWorker( context:

    Context, params: WorkerParameters ) : Worker(context, params) { override fun doWork(): Worker.Result { // Կ͔͠Βͷॲཧ return Worker.Result.success() } }
  2. Worker Data build setInputData set Worker getInputData WorkRequest // Workerʹ஋Λ༩͑Δ

    val data = Data.Builder().putString("KEY", "VALUE").build() OneTimeWorkRequest.Builder(MyWorker::class.java) .setInputData(data) .build() // WorkerͰ஋ΛऔΓग़͢ getInputData().getString("KEY")
  3. WorkManager#enqueue WorkRequest workManager = WorkManager.getInstance() val data = Data.Builder().putString("KEY", "VALUE").build()

    val request = OneTimeWorkRequest.Builder(MyWorker::class.java) .setInputData(data) .build() workManager.enqueue(request)
  4. WorkManager#beginWith Continuation then Work Continuation#enqueue Work // AWorker -> BWorker

    -> CWorkerͷॱʹ࣮ߦ͞Ε͍ͯ͘WorkContinuation val continuation = workManager.beginWith(OneTimeWorkRequest.from(AWorker::class.java)) continuation.then(OneTimeWorkRequest.from(BWorker::class.java)) continuation.then(OneTimeWorkRequest.from(CWorker::class.java)) continuation.enqueue()
  5. WorkManager#beginUniqueWork ExistingWorkPolicy REPLACE - KEEP - APPEND - Work -

    var continuation = workManager.beginUniqueWork( "unique_work_id", ExistingWorkPolicy.REPLACE, OneTimeWorkRequest.from(AWorker::class.java) )
  6. WorkRequest Constraint Work // ୺຤͕ॆిதͰ͋Δ੍໿ΛՃ͑Δ val constraints = Constraints.Builder() .setRequiresCharging(true)

    .build() continuation = continuation.then( OneTimeWorkRequest.Builder(AWorker::class.java) .setConstraints(constraints) .build() )