Usage

Task Services protects critical async operations from app backgrounding interruption

Essential Kit's Task Services wraps platform-specific background task APIs into a unified async/await interface. When users background your app, Task Services requests additional execution time from the OS to complete critical operations like saving game state or uploading data.

Table of Contents

Import Namespaces

Understanding Background Task Protection

When a user presses the home button or switches apps, mobile operating systems transition your app through lifecycle states:

Normal Flow (without Task Services):

Protected Flow (with Task Services):

Core API: AllowRunningApplicationInBackgroundUntilTaskCompletion

The primary method protects any async Task from backgrounding interruption:

Parameter
Type
Purpose

task

Task

The async operation to protect from backgrounding

onBackgroundProcessingQuotaWillExpireCallback

Callback

Optional callback when background time is about to expire

Returns: Continuation task that completes when the original task finishes and handles cleanup.

Generic Overload for Task<TResult>

For tasks that return values:

Preserves the task result while providing background protection.

Extension Methods for Cleaner Syntax

Task Services provides extension methods on Task and Task<TResult>:

API Signatures:

Extension methods provide identical functionality with more convenient syntax.

Pattern 1: Save Game Data on Pause

Protect save operations when the user backgrounds the app:

OnApplicationPause Timing: OnApplicationPause(true) is called when the app is about to background. This is your opportunity to initiate protected save operations.

Pattern 2: Extension Method Syntax

Use extension methods for cleaner, more readable code:

Pattern 3: Task with Return Value

Protect operations that return results:

Pattern 4: Multiple Protected Tasks

You can protect multiple independent tasks:

Handling Quota Expiration

Always implement quota expiration callbacks for graceful degradation:

Data Properties

Item
Type
Notes

TaskServices.AllowRunningApplicationInBackgroundUntilTaskCompletion

Method

Wraps a Task so Essential Kit requests platform background time until the work completes.

TaskServices.AllowRunningApplicationInBackgroundUntilTaskCompletion<TResult>

Method

Same as above but preserves the original task’s return value.

Task.AllowRunningApplicationInBackgroundUntilCompletion

Extension Method

Fluent syntax for protecting tasks without additional wrappers.

Callback onBackgroundProcessingQuotaWillExpireCallback

Callback

Invoked when the OS is about to reclaim background time; use it for emergency saves or cleanup.

TaskServicesUnitySettings.IsEnabled

bool

Confirms whether Task Services is active. If false, the helper methods will log an error instead of protecting the task.

Core APIs Reference

API
Purpose
Returns

TaskServices.AllowRunningApplicationInBackgroundUntilTaskCompletion(task, callback)

Protect async Task from backgrounding

Task continuation

TaskServices.AllowRunningApplicationInBackgroundUntilTaskCompletion<T>(task, callback)

Protect Task<T> preserving result

Task<T> continuation

task.AllowRunningApplicationInBackgroundUntilCompletion(callback)

Extension method for Task

Task continuation

task.AllowRunningApplicationInBackgroundUntilCompletion<T>(callback)

Extension method for Task<T>

Task<T> continuation

Error Handling

Scenario
Trigger
Recommended Action

Feature disabled or not initialized

Helper logs “Feature is not active or not yet initialised.”

Enable Task Services in Essential Kit Settings or call TaskServices.Initialize before awaiting background-protected tasks.

Background quota expires early

Long-running operation exceeds OS limit

Use the quota callback to persist critical state synchronously and resume the workload on the next foreground session.

Exceptions thrown inside protected task

await raises an exception after background completion

Wrap protected calls in try/catch blocks so you can surface telemetry, retry on resume, or notify the player.

Nested protection

Same task wrapped more than once

Guard against double-wrapping by tracking outstanding tasks or creating helper methods that centralize protection.

Android background restrictions

Device kills background work while in Doze/battery saver

Pair protected tasks with user-visible progress (e.g., foreground service) or postpone heavy uploads until the device is active.

Important Behaviors

Background Time Quota:

  • iOS: Approximately 30 seconds of background execution time

  • Android: Few minutes depending on device and Android version

  • System may terminate earlier if resources are critically low

Shared Quota:

  • All tasks protected in a single app share the same background time quota

  • Multiple AllowRunningApplicationInBackgroundUntilTaskCompletion calls don't multiply available time

Callback Timing:

  • onBackgroundProcessingQuotaWillExpireCallback is called shortly before system termination

  • Use this callback for emergency cleanup, not to extend execution time

Task Lifetime:

  • Protected tasks continue running even when the entire app is in background state

  • All other Unity MonoBehaviour code also continues executing during background time

Advanced: Manual Initialization

Understanding Manual Initialization

Default Behavior: Essential Kit automatically initializes Task Services using global settings from the ScriptableObject configured in Unity Editor.

Advanced Usage: Override settings at runtime when you need:

  • Custom background task timeout monitoring

  • Different task priority levels for various operations

  • Runtime task execution policies based on battery level or device capabilities

Implementation

Call Initialize() once before using Task Services features. Most games should use the standard setup configured in Essential Kit Settings instead.

  • Demo scene: Assets/Plugins/VoxelBusters/EssentialKit/Examples/Scenes/TaskServicesDemo.unity

  • Use with Cloud Services to sync game state on backgrounding

  • Pair with Network Services to verify connectivity before uploads

  • See Testing Guide for device testing checklist

Last updated

Was this helpful?