feat: add requiresDeviceIdle constraint + opportunistic-dispatch docs#37
Merged
Merged
Conversation
Add `WorkConstraints.requiresDeviceIdle` (previously parked as a v2 gap) and document the library's opportunistic-dispatch contract. requiresDeviceIdle is enforced natively on Android via `Constraints.Builder.setRequiresDeviceIdle` (JobScheduler / Doze maintenance windows) and surfaces as `PendingPredicate.RequiresDeviceIdle` through `scheduled()`. On iOS/macOS/JVM it is advisory: those systems have no device-idle knob (`BGProcessingTaskRequest` exposes only requiresExternalPower / requiresNetworkConnectivity) and already prefer idle moments themselves — iOS logs once, macOS/JVM stay silent-with-a- comment to match how `requiresCharging` is already treated in those files. This establishes the advisory-constraint template: enforce where native, accept-and-document elsewhere, never imitate (LESSONS D-025). Docs: new concepts/opportunistic-dispatch.md names the "interval is a floor, not a promise" contract and pre-answers the common "why didn't my background task run on time" confusions. Per-platform constraint honesty notes updated across android/ios/macos/jvm and the network-required recipe. minInterval/last-run throttling was considered and descoped — opportunistic dispatch already floors execution frequency. Verified: ./gradlew check + linkDebugFrameworkIosArm64 pass; mkdocs strict build and docs:check pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new cross-platform work constraint,
WorkConstraints.requiresDeviceIdle, and documents the library's opportunistic-dispatch contract — both in response to the question "how do we support 'run this when the OS gives the app space', and what is the Android equivalent of iOS background fetch?"Two deliverables:
requiresDeviceIdle: Boolean = false— "only dispatch when the device is idle." It was previously parked as a v2 gap in theWorkConstraintsKDoc; it now ships.docs/concepts/opportunistic-dispatch.md— names the contract that already governs every platform (intervals are floors, not promises; the OS picks the moment) and pre-answers the recurring "why didn't my background task run on time" confusions.Why
The "do this when the OS gives you space" behaviour was already the library's only mode —
BGTaskScheduler/WorkManager/NSBackgroundActivitySchedulerall dispatch opportunistically. What was missing was (a) a documented name for that contract so callers stop readingintervalas a wall-clock promise, and (b) the Android-idle constraint, which is the concrete "idle?" analogue the iOS background-fetch model implies.Platform behaviour (the honest table)
requiresDeviceIdlefollows the same advisory-constraint template asrequiresCharging:Constraints.Builder.setRequiresDeviceIdle(true)→JobScheduler(Doze / maintenance windows). Surfaces asPendingPredicate.RequiresDeviceIdleviascheduled().BGProcessingTaskRequesthas no idle property (onlyrequiresExternalPower/requiresNetworkConnectivity); the system already prefers idle moments. One-timelog.w, mirroring the existingNetworkRequirement.Unmeteredwarning.NSBackgroundActivitySchedulerhas no idle primitive; already biases idle viaqualityOfService = .background. Silent-with-comment.Reviewer notes
@Serializablecompatibility: new field has a default (= false) and is appended (existing field order unchanged) — serialization/binary compatible.PendingPredicate.RequiresDeviceIdle+ AndroidWorkInforead-back were wired at every touch-point thatRequiresChargingalready occupies (field →AndroidConstraintsMapper→AndroidScheduledTaskMapperpredicate + snapshot data class →PendingPredicate), to avoid the duplicate-drift trap this repo has hit before.requiresChargingas silent-with-a-doc-note; I matched the local convention rather than inventing a log they don't have. iOS does log because itsapplyConstraintsalready logs forUnmetered.WorkerContext/ clock changes. This adds no timing logic — N-011 (no wall-clock in scheduler math) and N-012 (noWorkerContextsingleton proxy) are not touched.minInterval/ last-run throttling was considered and descoped by the owner this session — opportunistic dispatch already floors execution frequency, so a throttle would be redundant machinery. Captured in LESSONS D-025 so it isn't re-proposed.network-required.md#require-the-device-to-be-idleanchor and is wired intomkdocs.ymlnav;mkdocs build --strict+ the repo'sdocs:checkboth pass.Verification
./gradlew check— PASS (82 tasks; all unit tests across JVM/Android/macOS/iOS-sim; no ktlint/detekt failures)./gradlew :backgrounder:linkDebugFrameworkIosArm64— PASS (iOS framework links clean)mise run docs:check+mkdocs build --strict— PASS🤖 Generated with Claude Code