If you need to send instantly, you can try OneTimeWorkRequest
.
Though its execution will also be dependent on the constraints you add or some other cases, but it'll start right after you call it most of the times.
Docs, Refer
Edit:
And then if you want this periodically
, You can create a PeriodicWorkRequest
and start your OneTimeWorkRequest
in it.
eg. Let's say you're having 2 methods,
startOneTimeWorker()
, startPeriodicWorker()
If you want to sync it once and instantly OneTimeWorker
will do the job.
But if you want to sync instantly and then schedule it also, call startOneTimeWorker()
, and then startPeriodicWorker()
. Where Periodic worker will be calling startOneTimeWorker()
in it's doWork()
So for the first time, OneTimeWorkRequest
will be called instantly and then it'll be called according to the schedule of PeriodicTimeWorkRequest
.
- Call
OneTimeWorkRequest
- Schedule
PeriodicWorkRequest
, calling OneTimeWorkRequest
in it