enable and pushSoon each set the CONNECTED constraint and the 30-second exponential backoff; B.online() on WorkRequest.Builder sets both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import androidx.work.NetworkType
|
|||||||
import androidx.work.OneTimeWorkRequestBuilder
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
import androidx.work.PeriodicWorkRequestBuilder
|
import androidx.work.PeriodicWorkRequestBuilder
|
||||||
import androidx.work.WorkManager
|
import androidx.work.WorkManager
|
||||||
|
import androidx.work.WorkRequest
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,8 +39,7 @@ object SyncSchedule {
|
|||||||
fun enable(context: Context) {
|
fun enable(context: Context) {
|
||||||
val request =
|
val request =
|
||||||
PeriodicWorkRequestBuilder<SyncWorker>(PERIOD_MINUTES, TimeUnit.MINUTES)
|
PeriodicWorkRequestBuilder<SyncWorker>(PERIOD_MINUTES, TimeUnit.MINUTES)
|
||||||
.setConstraints(networkRequired())
|
.online()
|
||||||
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, BACKOFF_SECONDS, TimeUnit.SECONDS)
|
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
// UPDATE, not KEEP: this is called on every launch, and KEEP would ignore
|
// UPDATE, not KEEP: this is called on every launch, and KEEP would ignore
|
||||||
@@ -68,8 +68,7 @@ object SyncSchedule {
|
|||||||
fun pushSoon(context: Context) {
|
fun pushSoon(context: Context) {
|
||||||
val request =
|
val request =
|
||||||
OneTimeWorkRequestBuilder<SyncWorker>()
|
OneTimeWorkRequestBuilder<SyncWorker>()
|
||||||
.setConstraints(networkRequired())
|
.online()
|
||||||
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, BACKOFF_SECONDS, TimeUnit.SECONDS)
|
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
// REPLACE rather than KEEP: if an earlier attempt is sitting in a long
|
// REPLACE rather than KEEP: if an earlier attempt is sitting in a long
|
||||||
@@ -79,11 +78,14 @@ object SyncSchedule {
|
|||||||
.enqueueUniqueWork(PUSH, ExistingWorkPolicy.REPLACE, request)
|
.enqueueUniqueWork(PUSH, ExistingWorkPolicy.REPLACE, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun networkRequired(): Constraints =
|
/** What both requests share: a network to run on, and the same backoff. */
|
||||||
Constraints
|
private fun <B : WorkRequest.Builder<B, *>> B.online(): B =
|
||||||
.Builder()
|
setConstraints(
|
||||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
Constraints
|
||||||
.build()
|
.Builder()
|
||||||
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
|
.build(),
|
||||||
|
).setBackoffCriteria(BackoffPolicy.EXPONENTIAL, BACKOFF_SECONDS, TimeUnit.SECONDS)
|
||||||
|
|
||||||
private const val PERIODIC = "inkwell-periodic-sync"
|
private const val PERIODIC = "inkwell-periodic-sync"
|
||||||
private const val PUSH = "inkwell-push-pending"
|
private const val PUSH = "inkwell-push-pending"
|
||||||
|
|||||||
Reference in New Issue
Block a user