Working with generic spatial accessories
At a glance
| Item | Summary |
|---|---|
| Purpose | Let people place digital replicas of a generic spatial accessory by tracking the accessory with ARKit. |
| App architecture | A Swift sample with the source-visible chain WorkingWithGenericSpatialAccessoriesApp → ContentView → AccessoryModel → SwiftUI / RealityKit APIs. |
| Main patterns | No named application pattern supported by the extracted structure |
| Project style | 12 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: Task, await suspension point, @MainActor, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, NotificationCenter, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, os, RealityKit, ARKit, Foundation; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── WorkingWithGenericSpatialAccessories/
└── WorkingWithGenericSpatialAccessories/
├── WorkingWithGenericSpatialAccessoriesApp.swift
├── Models/
│ ├── AppModel.swift
│ ├── HapticModel.swift
│ ├── AccessoryModel.swift
│ └── AccessoryModel+Haptics.swift
├── Views/
│ ├── ContentView.swift
│ ├── ImmersiveView.swift
│ ├── VolumeView.swift
│ ├── ToggleImmersiveSpaceButton.swift
│ └── ToggleVolumeButton.swift
└── Extensions/
├── Entity.swift
└── Logger.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 4 project/configuration file(s) and 13 source declaration(s).
Overall architecture
flowchart LR
N1["WorkingWithGenericSpatialAccessoriesApp"]
N2["ContentView"]
N3["AccessoryModel"]
N4["SwiftUI / RealityKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessoriesApp.swift:10 — architecture anchor
@main
struct WorkingWithGenericSpatialAccessoriesApp: App {
@State private var appModel = AppModel()
// ...
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into visionOS.
Ownership and state
classDiagram
WorkingWithGenericSpatialAccessoriesApp *-- AppModel : appModel
AppModel *-- AccessoryModel : accessoryModel
HapticModel o-- CHHapticEngine : hapticEngine
HapticModel *-- Logger : logger
Ownership evidence
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessoriesApp.swift:12 — stored dependency or nearest verified ownership anchor
@main
struct WorkingWithGenericSpatialAccessoriesApp: App {
@State private var appModel = AppModel()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
WorkingWithGenericSpatialAccessoriesApp |
AppModel (appModel) |
owns wrapper-managed state | Owning lexical scope |
AppModel |
AccessoryModel (accessoryModel) |
creates and retains | Owning type writes; wider scope can read |
HapticModel |
CHHapticEngine (hapticEngine) |
stores or receives | Owning lexical scope |
HapticModel |
Logger (logger) |
creates and retains | Initialized by the owner; the binding is immutable |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:40 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:41 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:132 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:132 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:40 — representative execution boundary
authChangeTask?.cancel()
authChangeTask = Task {
await handleAuthorizationStatusChange()
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | @Observable |
Observation macro publishes source-visible changes. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:16 |
| State propagation | NotificationCenter |
NotificationCenter distributes named process-local events. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:233 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ContentView.swift:16 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:12 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Logger.swift:8 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Entity.swift:8 |
| Source import | ARKit |
The cited file imports this module; runtime use and architectural role are not inferred. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Logger.swift:9 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessoriesApp.swift:11 — representative type boundary
@main
struct WorkingWithGenericSpatialAccessoriesApp: App {
@State private var appModel = AppModel()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
WorkingWithGenericSpatialAccessoriesApp |
Application entry and top-level composition | App |
AppModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
HapticModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
AccessoryModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
ContentView |
User-interface presentation and input forwarding | View |
ImmersiveView |
User-interface presentation and input forwarding | View |
VolumeView |
User-interface presentation and input forwarding | View |
ImmersiveSpaceState |
Represents mutable feature state | Concrete collaborators/imported frameworks |
Constants |
Defines a closed set of feature states or choices | Concrete collaborators/imported frameworks |
AccessorySettingsForm |
Represents a feature value or composable behavior | View |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
createSegment (WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Entity.swift:46) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
logger (WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:19) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
accessoryProviderTask (WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:22) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
authChangeTask (WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:23) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Entity.swift:46 — representative boundary
private static func createSegment(height: Float, radius: Float, color: UIColor) -> Entity {
let material = SimpleMaterial(color: color, isMetallic: false)
let mesh = MeshResource.generateCylinder(height: height, radius: radius)
return ModelEntity(mesh: mesh, materials: [material])
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | WorkingWithGenericSpatialAccessoriesApp |
The source’s App suffix makes this role explicit. |
| Feature data or observable state | AccessoryModel, AppModel, HapticModel |
The source’s Model suffix makes this role explicit. |
| User-interface presentation and input forwarding | ContentView, ImmersiveView, VolumeView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| No named application pattern | WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessoriesApp.swift:10 |
The verified source directly composes concrete framework types; this document avoids forcing a pattern name. |
Main application flow
sequenceDiagram
participant AccessoryModel
participant Task
participant AnchoringComponent
participant HapticModel
AccessoryModel->>Task: Start asynchronous work
AccessoryModel->>AccessoryModel: updateAccessoryTrackingProvider()
AccessoryModel->>Task: Start asynchronous work
AccessoryModel->>AnchoringComponent: AccessoryAnchoringSource()
AccessoryModel->>AccessoryModel: loadReferenceEntity()
AccessoryModel->>HapticModel: HapticModel()
Reference code
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift:282 — handleAccessoryDeviceChange()
private func handleAccessoryDeviceChange() {
// ...
availableLocations = []
hapticModel = nil
referenceEntity = nil
resetAnchorObservationState()
deviceChangeTask?.cancel()
Task {
await updateAccessoryTrackingProvider()
}
guard let accessoryDevice else {
return
}
accessoryDevice.input?.elementValueDidChangeHandler = { [weak self] (_, element) in
guard let self else { return }
if let button = element as? GCButtonElement,
button.pressedInput.isPressed {
logger.info("Button pressed: \(element.localizedName ?? "Unnamed element")")
initiateDigitalReplicaPlacement()
}
}
deviceChangeTask = Task {
do {
let anchoringSource = try await AnchoringComponent.AccessoryAnchoringSource(device: accessoryDevice)
guard !Task.isCancelled else { return }
availableLocations = anchoringSource.accessoryLocations
await loadReferenceEntity(from: anchoringSource)
} catch is CancellationError {
return
} catch {
logger.error("Failed to create anchoring source: \(error)")
}
hapticModel = await HapticModel(accessory: accessoryDevice)
}
}Naming conventions
- Types: App: WorkingWithGenericSpatialAccessoriesApp; Model: AccessoryModel, AppModel, HapticModel; View: ContentView, ImmersiveView, VolumeView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
play,createEvents,createPulseEvent,makeAnchoringSource,toggleAccessoryLocation,initiateDigitalReplicaPlacement,queryLatestAccessoryAnchor,observeAccessoryTrackingProvider. - Files:
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessoriesApp.swift,WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AppModel.swift,WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/HapticModel.swift,WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift,WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ContentView.swift,WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ImmersiveView.swift.
Architecture takeaways
WorkingWithGenericSpatialAccessoriesAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, RealityKit, ARKit, CoreHaptics through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessoriesApp.swift |
Cited implementation, WorkingWithGenericSpatialAccessoriesApp |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Entity.swift |
Cited implementation, RealityKit, MissingReferenceEntityConstants |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel.swift |
Cited implementation, Task, await suspension point, @MainActor, Task closure isolated to MainActor, @Observable, NotificationCenter, SwiftUI, ARKit, AccessoryModel |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ContentView.swift |
SwiftUI state property wrapper, ContentView, AccessorySettingsForm |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Extensions/Logger.swift |
os, Foundation, Feature implementation |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AppModel.swift |
AppModel, ImmersiveSpaceState |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/HapticModel.swift |
HapticModel, Constants |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ImmersiveView.swift |
ImmersiveView |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/VolumeView.swift |
VolumeView |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Models/AccessoryModel+Haptics.swift |
Feature implementation |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ToggleImmersiveSpaceButton.swift |
ToggleImmersiveSpaceButton |
WorkingWithGenericSpatialAccessories/WorkingWithGenericSpatialAccessories/Views/ToggleVolumeButton.swift |
ToggleVolumeButton |