Lesson 12 Repository Pattern and WorkManager
Lesson 12 Repository Pattern and WorkManager
Repository pattern
and WorkManager
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 12: Repository pattern and WorkManager
● Repository pattern
● WorkManager
● Work input and output
● WorkRequest constraints
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Repository pattern
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Existing app architecture
UI Controller
ViewModel
Room
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Relative data speeds
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Cache network responses
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Repository pattern
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
App architecture with repository pattern
UI Controller
ViewModel
Repository
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Implement a repository class
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
WorkManager
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
WorkManager
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
When to use WorkManager
Task
Requires active
Immediate user
Yes No
Exact Deferred
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Declare WorkManager dependencies
implementation "androidx.work:work-runtime-ktx:$work_version"
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Important classes to know
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Define the work
class UploadWorker(appContext: Context, workerParams: WorkerParameters) :
Worker(appContext, workerParams) {
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Extend CoroutineWorker instead of Worker
class UploadWorker(appContext: Context, workerParams: WorkerParameters) :
CoroutineWorker(appContext, workerParams) {
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
WorkRequests
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Schedule a OneTimeWorkRequest
Create WorkRequest:
val uploadWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<UploadWorker>()
.build()
WorkManager.getInstance(myContext)
.enqueue(uploadWorkRequest)
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Schedule a PeriodicWorkRequest
● Set a repeat interval
● Set a flex interval (optional)
Flex period Flex period Flex period
can run work can run work can run work
...
Interval 1 Interval 2 Interval N
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Flex interval
Work could And then again
happen here soon after
Example 1
Day 1 Day 2
1 hr 1 hr
Example 2
11 PM 12 AM 11 PM 12 AM
Day 1 Day 2
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
PeriodicWorkRequest example
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Enqueue periodic work
WorkManager.getInstance().enqueueUniquePeriodicWork(
"Unique Name",
ExistingPeriodicWorkPolicy.KEEP, // or REPLACE
repeatingRequest
)
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Work input and output
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Define Worker with input and output
class MathWorker(context: Context, params: WorkerParameters):
CoroutineWorker(context, params) {
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Result output from doWork()
Result.success() Result.success(output)
Result.failure() Result.failure(output)
Result.retry()
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Send input data to Worker
WorkManager.getInstance(myContext).enqueue(complexMathWork)
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
WorkRequest constraints
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Constraints
● setRequiredNetworkType
● setRequiresBatteryNotLow
● setRequiresCharging
● setTriggerContentMaxDelay
● requiresDeviceIdle
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Constraints example
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(true)
.setRequiresBatteryNotLow(true)
.setRequiresDeviceIdle(true)
.build()
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Summary
In Lesson 12, you learned how to:
● Use a repository to abstract the data layer from the rest of the app
● Schedule background tasks efficiently and optimize them using
WorkManager
● Create custom Worker classes to specify the work to be done
● Create and enqueue one-time or periodic work requests
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Learn more
● Fetch data
● Schedule tasks with WorkManager
● Define work requests
● Connect ViewModel and the repository
● Use WorkManager for immediate background execution
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 33