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

青春壹個敷衍的年華 提交于 2019-12-07 14:20:19

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/51586710/android-work-manager-does-work-manager-will-100-ensure-the-background-executi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!