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. WorkManager
    Yoshihiro Wada a.k.a. @e10dokup
    2019/1/28 at Potatotips #58

    View full-size slide

  2. a.k.a. e10dokup
    CyberAgent Inc.
    Ameba

    View full-size slide

  3. 2018/12/23 - 1.0.0-beta01
    2019/1/15 1.0.0-beta02
    2018/12/12 - 1.0.0-alpha13
    Breaking Change
    Codelabs
    WorkManager

    View full-size slide

  4. Android Jetpack
    compileSdkVersion 28
    API Play Services JobScheduler/AlermManager/
    FirebaseJobDispatcher
    etc
    WorkManager

    View full-size slide

  5. Worker
    WorkManager
    WorkRequest
    Worker Request WorkManager
    Request WorkManager
    WorkManager
    WorkRequest

    View full-size slide

  6. 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()
    }
    }

    View full-size slide

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

    View full-size slide

  8. OneTimeWorkRequest
    Worker Request
    PeriodicWorkRequest
    Worker Request
    WorkRequest

    View full-size slide

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

    View full-size slide

  10. 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()

    View full-size slide

  11. WorkManager#beginUniqueWork
    ExistingWorkPolicy
    REPLACE -
    KEEP -
    APPEND -
    Work -
    var continuation = workManager.beginUniqueWork(
    "unique_work_id",
    ExistingWorkPolicy.REPLACE,
    OneTimeWorkRequest.from(AWorker::class.java)
    )

    View full-size slide

  12. WorkRequest Constraint
    Work
    // ୺຤͕ॆిதͰ͋Δ੍໿ΛՃ͑Δ
    val constraints = Constraints.Builder()
    .setRequiresCharging(true)
    .build()
    continuation = continuation.then(
    OneTimeWorkRequest.Builder(AWorker::class.java)
    .setConstraints(constraints)
    .build()
    )

    View full-size slide

  13. NetWorkType -
    BatteryNotLow -
    Charging -
    StorageNotLow -
    DeviceIdle - API23 Doze Idle
    Work

    View full-size slide

  14. WorkRequest tag set WorkStatus
    uniqueId Continuation
    LiveData
    RxJava2 -> RxWorker 1.0.0-alpha12
    Codelabs
    2-3

    View full-size slide