Share
Embed
Transcript
45 slides
-
Options 4 Threads/Executors 4 Services 4 Alarm Manager 4 Async
Tasks 4 Download Manager Chicago Roboto 2019 4 -
Restrictions 4 Doze Mode/Battery Saver 4 Background Services Limitations 4
App Standby Buckets 4 Restricted Background Apps Chicago Roboto 2019 5 -
Work Manager The brand new magic tool to help you
perform Background Work seamlessly. Chicago Roboto 2019 6 -
The One With All The Features 4 API 14+ 4
Constraints 4 Multiple Chained Tasks 4 Guranteed Work Execution (Full Offline Mode Support) Chicago Roboto 2019 7 -
A sample is worth a thousand explanations 1 Me @
5 AM today Chicago Roboto 2019 8 -
Implementing Work Manager - Basic 4 Add Dependency 4 Extend
Worker class 4 Override doWork method 4 Return result 4 Schedule Work Chicago Roboto 2019 9 -
Implementing Work Manager - Basic The One With All The
Dependencies dependencies { def work_version = 2.0.0 // (Java only) implementation "android.arch.work:work-runtime:$work_version" // Kotlin + coroutines implementation "android.arch.work:work-runtime-ktx:$work_version" // optional - RxJava2 support implementation "android.arch.work:work-rxjava2:$work_version" // optional - Test helpers androidTestImplementation "android.arch.work:work-testing:$work_version" } Chicago Roboto 2019 10 -
Implementing Work Manager - Basic Extend Worker Class class SomeWorker(appContext:
Context, workerParams: WorkerParameters): Worker(appContext, workerParams) { override fun doWork(): Result { someWork() return Result.SUCCESS } } Chicago Roboto 2019 11 -
Implementing Work Manager - Basic Override doWork() class SomeWorker(appContext: Context,
workerParams: WorkerParameters): Worker(appContext, workerParams) { override fun doWork(): Result { someWork() return Result.SUCCESS } } Chicago Roboto 2019 12 -
Implementing Work Manager - Basic Return Result class SomeWorker(appContext: Context,
workerParams: WorkerParameters): Worker(appContext, workerParams) { override fun doWork(): Result { someWork() return Result.SUCCESS } } Chicago Roboto 2019 13 -
Implementing Work Manager - Basic Schedule Work val myWorkRequest =
OneTimeWorkRequest.Builder(SomeWorker) .build() WorkManager.getInstance().enqueue(myWorkRequest) Chicago Roboto 2019 14 -
Worker Class - Under The Hood Abstract class Extends ListenableWorker
startWork() -> doWork() Chicago Roboto 2019 15 -
Worker Class - Under The Hood @Override public final @NonNull
ListenableFuture<Payload> startWork() { mFuture = SettableFuture.create(); getBackgroundExecutor().execute(new Runnable() { @Override public void run() { Result result = doWork(); mFuture.set(new Payload(result, getOutputData())); } }); return mFuture; } Chicago Roboto 2019 16 -
Result - Under The Hood Enum SUCCESS, FAILURE, RETRY Chicago
Roboto 2019 17 -
The One With All The Work Request One Time Work
Request Periodic Work Request Chicago Roboto 2019 18 -
Periodic Work Request // Create a periodic work request with
30 mins as repeat interval val repeatInterval = 30 val somePeriodicWorkRequest = PeriodicWorkRequest.Builder(SomeWorker, repeatInterval, TimeUnit.MINUTES).build() Chicago Roboto 2019 20 -
The One With The TAGS Human Readable Logical Groups Multiple
Tags Chicago Roboto 2019 21 -
TAGS - Sample val someWorkRequest = OneTimeWorkRequest.Builder(SomeWorker) .addTag("Sync") .addTag("Job Status")
.build() val someOtherWorkRequest = OneTimeWorkRequest.Builder(SomeWorker) .addTag("Sync") .addTag("Driver Activity") .build() Chicago Roboto 2019 22 -
The One With The Constraints 4 Network 4 Battery 4
Storage 4 Device State Chicago Roboto 2019 23 -
Constraints - Sample private fun getWorkerConstraints(): Constraints { return Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true) .setRequiresCharging(true) .setRequiresDeviceIdle(false) .setRequiresStorageNotLow(false) .build() } Chicago Roboto 2019 24 -
The One Where It All Made Sense WorkManager.getInstance().beginWith(work1, work2, work3)
.then(work4) .then(work5) .then(work7) .enqueue() Chicago Roboto 2019 27 -
Complex chains/combines A | +----------+ | | B C |
+----+ | | D E Chicago Roboto 2019 28 -
Complex chains/combines A | +----------+ | | B C +----------+
| +----------+ | | D E Chicago Roboto 2019 30 -
Complex chains/combines Chain 1 Chain 2 A C | |
B D +----------+ | E Chicago Roboto 2019 32 -
Complex chains/combines WorkContinuation chain1 = WorkManager.getInstance() .beginWith(workA) .then(workB); WorkContinuation chain2
= WorkManager.getInstance() .beginWith(workC) .then(workD); WorkContinuation chain3 = WorkContinuation.combine(chain1, chain2) //Combines chain 1 and 2 .then(workE); chain3.enqueue(); Chicago Roboto 2019 33 -
The One With The Unique Work Ensure single instance of
same work val uniqueName = "MyUniqueName" val myWorkRequest = OneTimeWorkRequest.Builder(SomeWorker).build() WorkManager.getInstance().enqueueUniqueWork(uniqueName, ExistingWorkPolicy.REPLACE, myWorkRequest) Chicago Roboto 2019 34 -
Tips/Gotchas 4 Use Tags to limit scopes and group operations
together 4 Multiple chained jobs with different constraints can run at different times 4 Evaulate trigger points for all your workers Chicago Roboto 2019 41 -
Tips/Gotchas 4 One Time Work & Periodic Work share tags
4 Check isStopped() before doing work 4 Cancel all work before removing WM Chicago Roboto 2019 42 -
Summary 4 Runs Work On Background Thread 4 Guarantees execution
across system reboots 4 Runs work using best possible method based on API level of the user's device 4 Wavelock Management Chicago Roboto 2019 43 -
Summary - Under The Hood 4 Uses Room DB to
save data 4 Keeps snapshots of work being done, hence queryable 4 Restarts pending work 4 Runs your work on a separate thread but synchronously using a Single Threaded Executor Chicago Roboto 2019 44 -
The One With The Endgame 4 Reach out to me
at @AdnanM0123 4 Talk about WM or droidcon India or anything Android 4 Well, anything except Flutter/RN maybe ? 4 And.... Chicago Roboto 2019 45