$npx -y skills add dpearson2699/swift-ios-skills --skill metrickitUse when collecting or analyzing production iOS or iPadOS performance telemetry with MetricKit, including iOS 27 MetricManager async metric or diagnostic reports, hang or crash triage, custom signposts, extended launch measurement, durable export, or iOS 26 MXMetricManager compat
| 1 | # MetricKit |
| 2 | |
| 3 | Use MetricKit for low-overhead production telemetry that complements local Instruments and Xcode Organizer analysis. On iOS and iPadOS 27, prefer the Swift-first `MetricManager` report sequences. Keep `MXMetricManager` only in an explicit iOS 26 compatibility branch. |
| 4 | |
| 5 | > **Beta-sensitive:** The iOS/iPadOS 27 surface below is based on Apple's current beta documentation. It has not been locally compiler-verified because Xcode 27 is unavailable in this environment. Re-check the linked Apple documentation and compile with the shipping Xcode 27 SDK before release. |
| 6 | |
| 7 | Load [MetricKit Extended and Compatibility Patterns](references/metrickit-patterns.md) when implementing durable ingestion, detailed report analysis, or the iOS 26 compatibility path. |
| 8 | |
| 9 | ## Contents |
| 10 | |
| 11 | - [MetricManager Setup](#metricmanager-setup) |
| 12 | - [Receiving Metric Reports](#receiving-metric-reports) |
| 13 | - [Receiving Diagnostic Reports](#receiving-diagnostic-reports) |
| 14 | - [Key Metric Results](#key-metric-results) |
| 15 | - [Call Stack Trees](#call-stack-trees) |
| 16 | - [Custom Signpost Metrics](#custom-signpost-metrics) |
| 17 | - [Durable Export and Upload](#durable-export-and-upload) |
| 18 | - [Extended Launch Measurement](#extended-launch-measurement) |
| 19 | - [iOS 26 Compatibility](#ios-26-compatibility) |
| 20 | - [Xcode Organizer](#xcode-organizer) |
| 21 | - [Scope Boundaries](#scope-boundaries) |
| 22 | - [Common Mistakes](#common-mistakes) |
| 23 | - [Review Checklist](#review-checklist) |
| 24 | - [References](#references) |
| 25 | |
| 26 | ## MetricManager Setup |
| 27 | |
| 28 | At app launch, create and retain one long-lived `MetricManager`. Start exactly one consumer task for `metricReports` and one for `diagnosticReports`. |
| 29 | |
| 30 | Both properties expose nonthrowing `AsyncSequence` values: |
| 31 | |
| 32 | - `metricReports: some AsyncSequence<MetricReport, Never>` |
| 33 | - `diagnosticReports: some AsyncSequence<DiagnosticReport, Never>` |
| 34 | |
| 35 | Apple documents that concurrent consumers of one sequence can receive nondeterministic subsets. Fan out only after the single consumer receives and durably stores a report; delayed subscription can miss reports. |
| 36 | |
| 37 | ```swift |
| 38 | import MetricKit |
| 39 | |
| 40 | @available(iOS 27.0, *) |
| 41 | final class MetricsService { |
| 42 | private let manager = MetricManager() |
| 43 | private var metricTask: Task<Void, Never>? |
| 44 | private var diagnosticTask: Task<Void, Never>? |
| 45 | |
| 46 | func start( |
| 47 | persistMetric: @escaping @Sendable (MetricReport) async -> Void, |
| 48 | persistDiagnostic: @escaping @Sendable (DiagnosticReport) async -> Void |
| 49 | ) { |
| 50 | guard metricTask == nil, diagnosticTask == nil else { return } |
| 51 | let manager = manager |
| 52 | |
| 53 | metricTask = Task { |
| 54 | for await report in manager.metricReports { |
| 55 | await persistMetric(report) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | diagnosticTask = Task { |
| 60 | for await report in manager.diagnosticReports { |
| 61 | await persistDiagnostic(report) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | deinit { |
| 67 | metricTask?.cancel() |
| 68 | diagnosticTask?.cancel() |
| 69 | } |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | The persistence closures are application-specific. Implement them with the durable-first workflow below rather than dropping, logging only, or directly uploading each report. If state-scoped metrics are needed, construct the manager with the documented `init(enabledStateReportingDomains:)` initializer and the required domains. |
| 74 | |
| 75 | ## Receiving Metric Reports |
| 76 | |
| 77 | `MetricReport` is `Codable` and `Sendable`. It describes an interval through: |
| 78 | |
| 79 | - `timeRange: DateInterval` |
| 80 | - optional `environment` metadata |
| 81 | - `intervalEntries` for full-day and shorter interval measurements |
| 82 | - `stateEntries` for measurements associated with application states |
| 83 | |
| 84 | Metric reports normally arrive on a daily cadence. Persist the complete report before extracting individual results. |
| 85 | |
| 86 | For daily analysis, read the documented `fullDayEntry` and switch over its `MetricResult` values: |
| 87 | |
| 88 | ```swift |
| 89 | let entry = report.intervalEntries.fullDayEntry |
| 90 | |
| 91 | for result in entry.values { |
| 92 | switch result { |
| 93 | case .hangTime(let metric): |
| 94 | analyzeHangTime(metric) |
| 95 | case .peakMemory(let metric): |
| 96 | analyzePeakMemory(metric) |
| 97 | case .timeToFirstDraw(let metric): |
| 98 | analyzeLaunch(metric) |
| 99 | case .signpostInterval(let metric): |
| 100 | analyzeSignpost(metric) |
| 101 | @unknown default: |
| 102 | preserveUnknownMetric(result) |
| 103 | } |
| 104 | } |
| 105 | ``` |
| 106 | |
| 107 | Use `@unknown default` so a beta or future result does not make the ingestion pipeline brittle. Preserve the raw encoded report even when the current app does not understand a result. |
| 108 | |
| 109 | ## Receiving Diagnostic Reports |
| 110 | |
| 111 | `DiagnosticReport` is `Codable` and `Sendable`. It contains a `timeRange`, required `environment` metadata, and one `DiagnosticResult`. |
| 112 | |
| 113 | Diagnostics are individual, event-based reports intended for prompt delivery when MetricKi |