Skip to content

background ​

go
import "github.com/tangoui/tango/sys/background"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

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.

常量 ​

go
const (
	Refresh    = platform.BackgroundRefresh    // short, frequent (content refresh)
	Processing = platform.BackgroundProcessing // longer, typically while idle
)

变量 ​

go
var (
	ErrUnsupported   = platform.ErrUnsupported
	ErrNotConfigured = platform.ErrNotConfigured
)

函数 ​

func Cancel ​

go
func Cancel(a *tango.App, k Kind) error

Cancel withdraws the pending request of kind k.

func OnTask ​

go
func OnTask(a *tango.App, fn func(ctx context.Context, t Task) error)

OnTask sets the function that runs tasks (replacing an earlier one). It does nothing where the backend has no background tasks.

func Schedule ​

go
func Schedule(a *tango.App, r Request) error

Schedule asks the system to run a task of r.Kind, replacing a pending request of that kind.

类型 ​

Kind ​

go
type Kind = platform.BackgroundKind

Kind is the kind of a task. Each kind has at most one pending request.

Request ​

go
type Request = platform.BackgroundRequest

Request asks the system to run a task of Kind later.

Task ​

go
type Task struct {
	Kind     Kind
	Deadline time.Time // when the system stops it at the latest (zero: unknown)
}

Task describes a running task.

以 MIT 许可发布