background

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

Variables

View Source
var (
	ErrUnsupported   = platform.ErrUnsupported
	ErrNotConfigured = platform.ErrNotConfigured
)

Functions

func Cancel

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

Cancel withdraws the pending request of kind k.

func OnTask

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

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.

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.

type Task

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

Task describes a running task.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL