Documentation
¶
Overview ¶
Package background runs work while the app is in the background: periodic content refresh and longer maintenance tasks, scheduled with the system's task scheduler (BGTaskScheduler on iOS, JobScheduler on Android) and run when the system decides.
func main() {
app := tango.New(...)
background.OnTask(app, func(ctx context.Context, t background.Task) error {
return syncInbox(ctx) // stop when ctx is done
})
app.OnReady(func() {
go background.Schedule(app, background.Request{Kind: background.Refresh, Earliest: 15 * time.Minute})
...
})
app.Run()
}
The system may launch the app just to run a task: main and App.Run run as usual (OnReady included), but no page is on screen. Register OnTask in main, before Run, so tasks of such a launch are not missed. The task function runs on its own goroutine; its context is canceled when the system wants the task to stop (iOS gives a refresh about 30 seconds, Android up to 10 minutes) or at the deadline. Return nil for success. Requests are one-shot: schedule the next one from the task.
The app must declare the "background" capability in tan.json "permissions" (iOS: UIBackgroundModes fetch/processing and the task identifiers <id>.tango.refresh / <id>.tango.processing in Info.plist; Android: the host's JobService and RECEIVE_BOOT_COMPLETED). Without it Schedule returns ErrNotConfigured on iOS.
Testing on iOS: the simulator never runs BGTasks by itself. Pause the app in the debugger and run
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"<id>.tango.refresh"]
On Android: adb shell cmd jobscheduler run -f <applicationId> <job id> (1 refresh, 2 processing).
Schedule and Cancel return ErrUnsupported on desktop and tango.ErrNotRunning after the app quit.
Index ¶
Constants ¶
const ( Refresh = platform.BackgroundRefresh // short, frequent (content refresh) Processing = platform.BackgroundProcessing // longer, typically while idle )
Variables ¶
var ( ErrUnsupported = platform.ErrUnsupported ErrNotConfigured = platform.ErrNotConfigured )
Functions ¶
Types ¶
type Kind ¶
type Kind = platform.BackgroundKind
Kind is the kind of a task. Each kind has at most one pending request.
type Request ¶
type Request = platform.BackgroundRequest
Request asks the system to run a task of Kind later.