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):
System Limits: Task Services extends background time but cannot eliminate system limits. iOS allows ~30 seconds, Android allows a few minutes. Design tasks to complete within these limits or implement checkpoint/resume patterns.
Core API: AllowRunningApplicationInBackgroundUntilTaskCompletion
The primary method protects any async Task from backgrounding interruption:
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:
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:
Shared Background Quota: All protected tasks share the same background time quota (30s on iOS, minutes on Android). Design tasks to complete within this limit or they may all be terminated together.
Handling Quota Expiration
Always implement quota expiration callbacks for graceful degradation:
Data Properties
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
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
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
AllowRunningApplicationInBackgroundUntilTaskCompletioncalls don't multiply available time
Callback Timing:
onBackgroundProcessingQuotaWillExpireCallbackis called shortly before system terminationUse 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
Best Practice: Design tasks to complete in under 20 seconds to have safety margin before iOS quota expiration. For longer operations, implement checkpoint/resume patterns.
Advanced: Manual Initialization
Manual initialization is only needed for specific runtime scenarios. For most games, Essential Kit's automatic initialization handles everything. Skip this section unless you need runtime configuration or custom task monitoring.
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
Related Guides
Demo scene:
Assets/Plugins/VoxelBusters/EssentialKit/Examples/Scenes/TaskServicesDemo.unityUse 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?