3

When my user logs out of my app, I have to cancel scheduled work that would require him to be signed in. Unfortunately, that means the scheduled work is lost, e.g. syncing some data to the server that was edited while the user was offline.

My idea was to defer the work that is currently scheduled until the user has logged in again. Alternatively, as a work around I could set the initial delay to something like 50 years and change the delay back after the user has logged in. However, as far as I am aware none of both is possible with WorkManager at the moment.

Does anyone have any ideas of how to approach this? Or can we expect this kind of feature to be provided by WorkManager in the future maybe?

DanD
  • 333
  • 3
  • 15
  • So you want to stop worker when user logout from your app? – Pratik Butani Jun 07 '19 at 13:35
  • Exactly, but I want to be able to restart it later. – DanD Jun 07 '19 at 17:50
  • There's no way to stop and restart a work in WorkManager, and you should consider what happens to an already running work. Depending on how you create/schedule your WorkRequest, you may be able to observe the status of your WorkRequest, be notified when they are cancelled, and plan to reschedule them in the future. – pfmaggi Jun 07 '19 at 20:22
  • Yes @pfmaggi is right, or else you can execute in between particular time. Check this : https://stackoverflow.com/a/56326758/1318946 You can edit this as per your requirement. – Pratik Butani Jun 08 '19 at 04:52

0 Answers0