Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Work Manager - Does Work Manager will 100% ensure the background execution to be Completed?

From what I read from the documentation https://developer.android.com/topic/libraries/architecture/workmanager,

It said that:

The task is still guaranteed to run, even if your app is force-quit or the device is rebooted.

So which mean, the execution that being at the background will 100% be execute until it complete no matter what?

For an example case:

An Apps have Button that perform Work Manager Implementation that upload data to an Online Database but it required Internet Connection to upload the data. So, my Apps is currently in Offline Mode and I click the Button.

My Uncertainty:

Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?

like image 299
I am a Student Avatar asked Jul 30 '18 03:07

I am a Student


People also ask

What is a workmanager in Android?

What is a WorkManager? WorkManager is part of Android Jetpack libraries and an Architecture Component mainly intended for doing long-running background tasks. Background tasks can be categorized into 3 buckets mainly depending on the time of execution

What does workmanager do for background work?

As a bonus, WorkManager takes care of handling the service’s lifecycle for you. And, the ten minute time limit for background work won’t apply to the work you’re doing in a worker running in a foreground service. Let’s take a look at how to make an existing worker execute work in a foreground service.

When should I use workmanager?

The WorkManager library is a good choice for tasks that are useful to complete, even if the user navigates away from a particular screen or your app. Some examples of tasks that are a good use of WorkManager: WorkManager offers guaranteed execution, and not all tasks require that.

What is guaranteed execution in workmanager?

Guaranteed execution means that WorkManager will take care of the logic to start your work under a variety of situations, even if you navigate away from your app. WorkManager is a simple, but incredibly flexible library that has many additional benefits. These include: WorkManager sits on top of a few APIs such as JobScheduler and AlarmManager.


2 Answers

Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?

It won't implicitly try to execute the work continuously and stop only if it was successful. It will depend on Result returned by doWork() or your Worker. If it returns RETRY, then the work will be retried with backoff specified in WorkRequest.Builder.setBackoffCriteria(BackoffPolicy, long, TimeUnit).

However, if you need something to be executed when there is internet connection, then you can specify the appropriate constraints. For network connectivity, you can set constraints as follows:

Constraints myConstraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();

 OneTimeWorkRequest mywork=
            new OneTimeWorkRequest.Builder(MyWorker.class)
 .setConstraints(myConstraints)
 .build();
 WorkManager.getInstance().enqueue(mywork);

WorkManager will ensure that your work is executed only if there is internet connection.

like image 83
Sagar Avatar answered Nov 15 '22 00:11

Sagar


Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?

So because you've specified that a work manager requires Network Connection.

val constraints = Constraints.Builder()
                            .setRequiredNetworkType(NetworkType.CONNECTED).build()

val workRequest = OneTimeWorkRequest.Builder(RequestWorker::class.java)
                            .setConstraints(constraints)
                            .build()

The work request will not be triggered until there is a Network Connection, see it as the WorkManager listening for a change in NetworkState and once the Network is CONNECTED it starts processing your workRequest(doWork).

I must also add that while your phone is on AirPlane mode and your phone reboots with a pending work request, i currently don't think the work request is guaranteed to start processing immediately as you might expect, even when NetWork state is CONNECTED.

like image 32
Paul Okeke Avatar answered Nov 14 '22 23:11

Paul Okeke