Contents

trackLaunchTask(id:onTrackingError:_:)

Measures the duration of an asynchronous extended launch task.

Declaration

@MainActor final func trackLaunchTask<Result, Failure>(id: LaunchTaskID, onTrackingError: ((MetricManager.LaunchTaskError) -> Void)? = nil, _ operation: () async throws(Failure) -> Result) async throws(Failure) -> Result where Failure : Error

Mentioned in

Discussion

Use this method to wrap asynchronous work that extends your app’s perceived launch time, such as fetching configuration data, loading initial content, or initializing a local database. Pass a LaunchTaskID and an async closure wrapping the work to measure. Measurement begins when the closure starts and ends it when the closure returns, regardless of whether the closure throws.

await manager.trackLaunchTask(id: "initial-data-load") {
    await loadInitialData()
}

Pass an onTrackingError closure to observe MetricManager.LaunchTaskError values without interrupting the tracked work:

await manager.trackLaunchTask(id: "initial-data-load", onTrackingError: { error in
    logger.warning("Tracking error: \(error.reason)")
}) {
    await loadInitialData()
}

This method replaces the paired MXMetricManager.extendLaunchMeasurement(forTaskID:) and MXMetricManager.finishExtendedLaunchMeasurement(forTaskID:) calls. To track synchronous launch work instead, use trackLaunchTask(id:onTrackingError:_:).

See Also

Extended launch