๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Dev/Android

[Android] WorkManager๋ฅผ ์ด์šฉํ•œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…

by JUNE.C 2021. 7. 25.

๐Ÿง  WorkManager?

 ์•ˆ๋“œ๋กœ์ด๋“œ๊ฐ€ ๋ฒ„์ „์—…์„ ๊ฑฐ๋“ญํ•˜๋ฉฐ ์–ด๋Š ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์ด ์ˆ˜์›”ํ•˜์ง€ ์•Š๊ฒŒ๋˜์—ˆ๋‹ค.

์ดˆ๊ธฐ์—๋Š” Service๋ฅผ ์ด์šฉํ•ด์„œ ์ž‘์—…ํ•˜๊ฑฐ๋‚˜ Broadcast Receiver๋ฅผ ํ†ตํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊นจ์šฐ๋Š” ๊ฒƒ ๋˜ํ•œ ์‰ฝ๊ฒŒ ๊ฐ€๋Šฅํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์•ˆ๋“œ๋กœ์ด๋“œ M(API 23)๋ถ€ํ„ฐ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์„ ์œ„ํ•œ ์ œ์•ฝ ์‚ฌํ•ญ๋“ค์ด ์ถ”๊ฐ€๋˜๋ฉฐ, ๊ฐœ๋ฐœ์ž๋“ค์€ ์—ฌ๋Ÿฌ API๋ฅผ ํ†ตํ•˜์—ฌ ์ถ”๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ์ด ํ•„์š”ํ•ด์กŒ๋‹ค. ์ฆ‰, ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๊นŒ๋‹ค๋กœ์›Œ์กŒ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

 ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. AlarmManager, JobScheduler, JobDispatcher(Firebase), ๊ทธ๋ฆฌ๊ณ  ์ง€๊ธˆ ์•Œ์•„๋ณผ WorkManager.

์—ฌ๊ธฐ์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” ์šฉ๋„(๋ชฉ์ )์— ๋”ฐ๋ผ์„œ ๊ฐœ๋ฐœ์ž๋“ค์ด ์ ์ ˆํ•œ API๋ฅผ ์„ ํƒํ•ด ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ตฌ๊ธ€์—์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ๊ฐ€์ด๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ํ•ด๋‹น ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์—ฌ ์•Œ๋งž์€ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์ž. ๊ณต์‹๋ฌธ์„œ - ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๊ฐ€์ด๋“œ

 

 WorkManager๋Š” ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด ๋ฐ˜๋“œ์‹œ ์ž‘์—…์„ ์‹คํ–‰ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ž‘์—…์ด ์ฆ‰์‹œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜์ง€ ์•Š์Œ์œผ๋กœ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ ํ•ฉํ•œ ์†”๋ฃจ์…˜์„ ๊ณ ๋ฏผํ•ด์•ผํ•œ๋‹ค.

 ์„œ๋ฒ„์— ๋กœ๊ทธ ๋˜๋Š” ๋ถ„์„ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ฑฐ๋‚˜ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์™€ ๋™๊ธฐํ™”ํ•˜๋Š” ์ž‘์—…, ์ด๋ฏธ์ง€ ์ €์žฅ ๋ฐ ์—…๋กœ๋“œ ๋“ฑ์˜ ์ž‘์—…์ด๋ผ๋ฉด WorkManager ์‚ฌ์šฉ์„ ์„ ํƒํ•ด๋„ ์ข‹๋‹ค.

 


๐Ÿ“„  WorkManager ๊ตฌ์„ฑ

  • Worker
    • Abstract class
    • ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ doWork() ๋ฉ”์†Œ๋“œ์— ์ •์˜ํ•œ๋‹ค.
    • ์ž‘์—… ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Result์— ์ •์˜๋œ success(), failure(), retry() ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      ๋ฐ˜ํ™˜๋œ ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ดํ›„ ๋™์ž‘์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. (์žฌ์‹œ๋„, ์ค‘๋‹จ ๋“ฑ)
      • Result.Sucesss / Result.Failure / Result.Retry
  • WorkRequest
    • Worker์— ์ •์˜๋œ task๋ฅผ ์ž‘๋™์‹œํ‚ค๊ธฐ ์œ„ํ•œ request๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • WorkRequest๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋ฐ˜๋ณต ์—ฌ๋ถ€, ์ œ์•ฝ ์‚ฌํ•ญ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค.
    • OneTimeWorkRequest
      • ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰ํ•  ์ž‘์—… ์š”์ฒญ์„ ๋‚˜ํƒ€๋‚ด๋Š” WorkRequest
    • PeriodicWorkRequest
      • ์ผ์ • ์ฃผ๊ธฐ๋กœ, ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰ํ•  ์ž‘์—… ์š”์ฒญ์„ ๋‚˜ํƒ€๋‚ด๋Š” WorkRequest
  • WorkManager
    • ์‹ค์ œ๋กœ WorkRequest๋ฅผ ์Šค์ผ€์ค„๋งํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ฉฐ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค
    • ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ›์•„์™€ WorkRequest๋ฅผ ํ์— ์ถ”๊ฐ€ํ•˜์—ฌ ์‹คํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค.

 


๐Ÿ”จ  ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ“Œ Dependency ์ถ”๊ฐ€

dependencies {
    implementation "androidx.work:work-runtime-ktx:2.5.0" // Kotlin + Coroutines
}

 

Java Only ๋ฒ„์ „ ๋˜๋Š” ์ตœ์‹  ๋ฒ„์ „์€ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณต์‹ ๋ฌธ์„œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

 

 

๐Ÿ“Œ Worker ์ž‘์„ฑํ•˜๊ธฐ

 Worker๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Context์™€ WorkerParameters๋ฅผ ์ƒ์„ฑ์ž์— ๋„˜๊ธด๋‹ค.

 

 doWork() ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๊ณ , ํ•˜๊ณ ์žํ•˜๋Š” ์ž‘์—… ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

class SomeWorker(ctx: Context, params: WorkerParameters) :
    Worker(ctx, params) { // If you want coroutines, CoroutineWorker()

    override fun doWork(): Result {
        return try {
            Timber.d("I'm hard worker, but i will sleep") // Timber is library for logging
            Thread.sleep(3_000)
            Timber.d("Who woke up. I'm tired.")

            Result.success() // return statement
        } catch (e: Exception) {
            Timber.e("Worker Exception $e")
            Result.failure() // return statement
        }

    }
}

 

 

๐Ÿ“Œ WorkerRequest ์ž‘์„ฑํ•˜๊ธฐ

 ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋  ์ž‘์—…์€ OneTimeWorkRequest, ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰๋  ์ž‘์—…์€ PeriodicWorkRequest๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.

 PeriodicWorkRequest์˜ ๊ฒฝ์šฐ ๋ฐ˜๋ณต ์ฃผ๊ธฐ ๋ฐ ์‹œ๊ฐ„ ๋‹จ์œ„๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์•ผ ํ•˜๋Š”๋ฐ ์ตœ์†Œ๊ฐ’์€ 15๋ถ„์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

 

 

// in view(activity, fragment) or viewmodel

// OneTimeWorkRequest
val workRequest = OneTimeWorkRequestBuilder<SomeWorker>().build()

// PeriodicWorkRequest
val workRequest = PeriodicWorkRequestBuilder<SomeWorker>(15, TimeUnit.MINUTES).build()

 

 

๐Ÿ“Œ WorkerManager ์ž‘์„ฑ ๋ฐ ํ์— ์ž‘์—… ์ถ”๊ฐ€ํ•˜๊ธฐ

  WorkerManager์˜ ์ธ์Šคํ„ด์Šค๋Š” getInstance() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 ๊ทธ๋ฆฌ๊ณ  enqueue() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด WorkRequest๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

// in view(activity, fragment) or viewmodel

private val workManager = WorkManager.getInstance(application)

workManager.enqueue(workRequest) // ์œ„์—์„œ ์ž‘์„ฑํ•œ workRequest๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

 

 ์œ„์™€ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ WorkManager์— ์ž‘์—…๋“ค์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

 


๐Ÿ”จ  ๋” ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“Œ Constraint(์ œ์•ฝ ์‚ฌํ•ญ) ์ถ”๊ฐ€ํ•˜๊ธฐ

 WorkRequestBuilder์— ์ œ์•ฝ ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ œ์•ฝ ์‚ฌํ•ญ์€ Constraints.Builder()๋ฅผ ํ†ตํ•ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 ๋„คํŠธ์›Œํฌ ์œ ํ˜•, ์ถฉ์ „ ์ƒํƒœ ๋“ฑ์— ๋”ฐ๋ผ์„œ WorkManager๋ฅผ ์ž‘๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋” ์ž์„ธํ•œ ์ œ์•ฝ ์กฐ๊ฑด ์ข…๋ฅ˜๋Š” ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์ž.
๊ณต์‹ ๋ฌธ์„œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

 

// in view(activity, fragment) or viewmodel

val constraints = Constraints.Builder()
            .setRequiresCharging(true) // ์ถฉ์ „ ์ƒํƒœ์ผ ๋•Œ๋งŒ ์ž‘๋™ํ•œ๋‹ค.
            .setRequiresBatteryNotLow(true) // ๋ฐฐํ„ฐ๋ฆฌ ๋ถ€์กฑ์ƒํƒœ๊ฐ€ ์•„๋‹ ๋•Œ๋งŒ ์ž‘๋™ํ•œ๋‹ค.
            .build()

val workRequest = OneTimeWorkRequestBuilder<SomeWorker>()
            .setConstraints(constraints)
            .build()

 

 

๐Ÿ“Œ ํƒœ๊ทธ ์ถ”๊ฐ€ ๋ฐ ์ž‘์—… ๊ด€๋ฆฌํ•˜๊ธฐ

 ๋ชจ๋“  ์ž‘์—… ์š”์ฒญ์— ๊ณ ์œ  ์‹๋ณ„์ž์ธ ํƒœ๊ทธ๋ฅผ ๋„ฃ์–ด ํ•ด๋‹น ์ž‘์—…์„ ์ทจ์†Œํ•˜๊ฑฐ๋‚˜ ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

WorkRequestBuilder์— addTag() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

 ํƒœ๊ทธ ๊ฐ’์„ ํ†ตํ•˜์—ฌ ์ทจ์†Œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž‘์—… ์ง„ํ–‰ ์ƒํ™ฉ์€ ๋ฐ˜ํ™˜๋˜๋Š” WorkInfo ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • WorkInfo State
    • BLOCKED
    • CANCELLED
    • ENQUEUED
    • FAILED
    • RUNNING
    • SUCCEEDED

  ํ•ด๋‹น ๊ฐ์ฒด๋Š” LiveData๋กœ ๋ฐ˜ํ™˜๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ƒํƒœ์— ๋”ฐ๋ผ์„œ progress๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜์žˆ๋‹ค.

 

// add tag
val workRequest = OneTimeWorkRequestBuilder<SomeWorker>()
	.addTag("some_worker") // Add this method
    .build()
    
// cancel work
workManager.cancelAllWorkByTag("some_worker")

// get work info to livedata
val workInfos = workManager.getWorkInfosByTagLiveData("some_worker")

// observe example in view
workInfos.observe(lifecycleOwner) { workInfos ->
    if (listOfWorkInfo.isNullOrEmpty()) return@observe
    val workInfo = workInfos[0]
    
    if (workInfo.state.isFinished) { // SUCCEEDED or FAILED or CANCELLED
    	hideProgressBar()
    } else {
    	showProgressBar()
    }
}

 

 

๐Ÿ“Œ ์—ฌ๋Ÿฌ ์ž‘์—… ์ฒด์ด๋‹ํ•˜๊ธฐ

 ์ž‘์—…์„ ์ˆœ์ฐจ์  ๋˜๋Š” ๋™์‹œ์— ์‹คํ–‰๋˜๋„๋ก ๋งŒ๋“œ๋Š” WorkRequest๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

Work1๊ณผ Work2 ํ…Œ์Šคํฌ๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๊ณ  ํ•ด๋‹น ์ž‘์—…์ด ๋๋‚œ ์ดํ›„์— Work3, Work4๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค.

Chain WorkRequest Example

 ์šฐ์„  ์œ„ ํ…Œ์Šคํฌ๋ฅผ ์ •์˜ํ•œ Worker๋“ค์„ WorkRequest๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 enqueue() ๋ฉ”์†Œ๋“œ ๋‚ด์— WorkRequest๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, beginWith() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์‹œ์ž‘ํ•  ์ž‘์—…์„ ๋„˜๊ธด๋‹ค. ์ด๋•Œ ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ž‘์—…์„ ๋™์‹œ์— ์‹œ์ž‘ํ•˜๋ ค๋ฉด List์— ๋‹ด์•„ ๋„˜๊ฒจ์ค€๋‹ค.

์œ„ ์ž‘์—…์ด ๋๋‚˜๋ฉด ์‹œ์ž‘ํ•  ์ž‘์—…๋“ค์„ then() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ž‘์„ฑํ•œ๋‹ค. then() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ณ„์† ์ฒด์ด๋‹ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž‘์—… ํ์— ๋ณด๋‚ผ ํ…Œ์Šคํฌ๋ฅผ ๋ชจ๋‘ ์ž‘์„ฑํ•˜์˜€์œผ๋ฉด, enqueue() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ์— ์ตœ์ข…์ ์œผ๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ ์ž‘์—…์„ ์‹คํ–‰ํ•œ๋‹ค.

 

val work1 = OneTimeWorkRequestBuilder<Work1Worker>().build()
// ... val work2 ~

// workManager is WorkManager Instance.
workManager
    .beginWith(listOf(work1, work2))
    .then(work3)
    .then(work4)
    .enqueue()

 

 

๐Ÿ“Œ Worker์™€ ๋ฐ์ดํ„ฐ ์ฃผ๊ณ ๋ฐ›๊ธฐ

 Key-Value ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ Worker์˜ Data๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„ Worker๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

 WorkRequest์— setInputData() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด Data๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ Data๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” Data.Builder()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 Worker ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์ธ WorkerParameters๋กœ ๋ฐ์ดํ„ฐ๋Š” ์ „๋‹ฌ๋˜๋ฉฐ, getInputData()[=inputData in Kotlin] ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 ์ž‘์—…์„ ์™„๋ฃŒ(์„ฑ๊ณต)ํ•œ ํ›„์— Result.success() ๋ฉ”์†Œ๋“œ์— Data๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

 

 Data๋ฅผ ๋งŒ๋“ค ๋•Œ Data.Builder()๋ฅผ ํŽธํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ธ workDataOf()๋„ ์กด์žฌํ•˜๋ฉฐ, ์•„๋ž˜ Worker ํด๋ž˜์Šค์—์„œ ํ™•์ธํ•ด๋ณด์ž.

๋นŒ๋”๋ฅผ ํ†ตํ•œ ์ƒ์„ฑ์€ WorkRequest์— Inputํ•  ๋•Œ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 Worker์—์„œ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋Š” WorkInfo์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

// input data for worker
val inputData = Data.Builder().apply {
        putString("secret_key", "genius-dev")
        build()
	}

val workRequest = OneTimeWorkRequestBuilder<SomeWorker>()
    .setInputData(inputData)
    .build()

workManager.enqueue(workRequest)


// in Worker
class BlurWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params) {
    override fun doWork(): Result {
    	val secretKey = inputData.getString("secret_key")
        
        //..
        
        val outputData = workDataOf("public_key" to "android")
        
        Result.success(outputData)
    }
}

// in view(activity, fragment) or viewmodel
workInfos.observe(lifecycleOwner) { workInfos ->
    if (listOfWorkInfo.isNullOrEmpty()) return@observe
    val workInfo = workInfos[0]
    
    if (workInfo.state.isFinished) {
    	hideProgressBar()
        
        val outputData = workInfo.outputData.getString("public_key") // <<<< HERE
        
        Toast.makeText(this, "worker output data : $outputData", Toast.LENGTH_SHORT).show()
    } else {
    	showProgressBar()
    }
}

 

 


๐Ÿ“• References

๊ณต์‹ ๋ฌธ์„œ - WorkManager๋กœ ์ž‘์—… ์˜ˆ์•ฝ

Android Codelabs - WorkManager

๊ณต์‹ ๋ฌธ์„œ - ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ฒ˜๋ฆฌ ๊ฐ€์ด๋“œ

GM.Lim(@limgyumin) Blog

๊ณต์‹ ๋ฌธ์„œ - ์ž‘์—… ์š”์ฒญ ์ •์˜

 

๋Œ“๊ธ€