Documentation
¶
Overview ¶
Package background runs an app's Go code while it isn't open, as the phone finds the time: syncing a feed overnight, uploading what waited for a network. BGTaskScheduler on iOS, WorkManager on Android; elsewhere Schedule reports errors.ErrUnsupported.
The system starts the app's process to run a task, without showing it, and main runs as ever: call Handle before App.Run, so that the handler is there by then.
background.Handle("sync", func(ctx context.Context) error {
return syncFeed(ctx)
})
mygo.App.WhenReady(func() {
go background.Schedule(context.Background(), "sync", background.Options{
Every: 4 * time.Hour, Network: true,
})
})
When a task runs is the system's choice: after Delay, around Every, with what it knows of the battery and of how much the user opens the app. iOS gives a task about 30 seconds, or minutes with Long; Android about 10 minutes. A handler stops when its context ends. A repeating task outlives the process and reboots until Cancel; one that runs once does until it succeeds.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handle ¶
Handle says what a task does, before App.Run. It runs on a goroutine of its own, off the main thread. A task whose handler returns an error runs again later.
Types ¶
type Options ¶
type Options struct {
// Every repeats it, at least 15 minutes apart on either system. Without it
// the task runs once.
Every time.Duration
// Delay is the least it waits before it runs the first time.
Delay time.Duration
// Network waits for a network; on iOS, for Long tasks only, as the system
// decides when others run.
Network bool
// Charging waits for the phone to charge; on iOS, for Long tasks only.
Charging bool
// Long asks for minutes, which on iOS come rarely, overnight and charging,
// rather than half a minute.
Long bool
}
Options say when a task may run.