StateReporter
An object unique per domain that records state transitions and volatile metadata updates.
Declaration
final class StateReporter<StableMetadata, VolatileMetadata> where StableMetadata : ReportableMetadata, VolatileMetadata : ReportableMetadataMentioned in
Overview
StateReporter is the central object for recording your feature’s or subsystem’s current state. You obtain an instance through the reporter(for:stableMetadata:volatileMetadata:) method, which guarantees that every caller using the same domain string receives the same object. Attempting to call the method with different generic type arguments for an already-registered domain is a fatal error.
A state is uniquely identified by the combination of a label and stable metadata. A transition to a new state occurs when either changes; reporting the same label and stable metadata is a no-op. Volatile metadata provides additional context within an ongoing state and is discarded when the next transition begins. Both stable and volatile metadata are expressed as types conforming to ReportableMetadata, which can be synthesized automatically with the ReportableMetadata() macro.
Call reportTransition(to:stableMetadata:volatileMetadata:) whenever your feature transitions to a new state. Pass nil as the label to signal that no state is active. Call reportVolatileMetadataUpdate(_:) to update volatile metadata without beginning a new state transition. Calling either method more frequently than user interaction timescales can trigger rate limiting, causing state updates to go unlogged.
let reporter = StateReporter.reporter(
for: "com.example.myapp.checkout",
stableMetadata:AppMetadata.self,
volatileMetadata:SessionMetadata.self
)
reporter.reportTransition(
to: "paymentSheet",
stableMetadata: AppMetadata(userTier: .premium),
volatileMetadata: SessionMetadata(cartTotal: 49.99)
)For Objective-C, use SRStateReporter.