Sample CodetvOSReviewed 2026-07-21View on Apple Developer

Supporting Multiple Users in Your tvOS App

At a glance

Item Summary
Purpose Store separate data for each user with the new Runs as Current User capability.
App architecture A Swift sample with the source-visible chain MemoryGameAppContentViewCoreDataStoreSwiftUI / CoreData APIs.
Main patterns Central store
Project style 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue.main.asyncAfter; none alone proves a background thread.
State/event model Source-visible mechanisms: ObservableObject, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, CoreData, os; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── MemoryGame/
    ├── MemoryGameApp.swift
    ├── GameView.swift
    ├── TileView.swift
    ├── ContentView.swift
    ├── CoreDataStore.swift
    ├── DebugCoreDataView.swift
    ├── DynamicFetchRequestView.swift
    ├── MainMenuView.swift
    ├── NewGameView.swift
    ├── ProminentHighScoreView.swift
    ├── SectionHeaderView.swift
    └── StandardHighScoreView.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 6 project/configuration file(s) and 17 source declaration(s).

Overall architecture

Reference code

MemoryGame/MemoryGameApp.swift:10 — architecture anchor

@main
struct MemoryGameApp: App {
    @StateObject private var store = CoreDataStore()

    var body: some Scene {
        WindowGroup {
            ContentView().environment(\.managedObjectContext, store.viewContext)
        }
    }
}

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 TV Services.

Ownership and state

Ownership evidence

MemoryGame/MemoryGameApp.swift:12 — stored dependency or nearest verified ownership anchor

@main
struct MemoryGameApp: App {
    @StateObject private var store = CoreDataStore()
    // ...
}
Owner Object or state Relationship Mutation authority
MemoryGameApp CoreDataStore (store) owns wrapper-managed state Owning lexical scope
GameView GameEntity (game) observes externally owned state The observed object is authoritative
TileView String (sticker) owns value state App/module collaborators
TileView TileState (state) stores or receives App/module collaborators

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
Queue scheduling DispatchQueue.main.asyncAfter The source addresses the main dispatch queue. MemoryGame/GameView.swift:84

@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

MemoryGame/GameView.swift:84 — representative execution boundary

                    DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                        withAnimation(.spring()) { state = .none }
                    }

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 ObservableObject ObservableObject supplies an observation contract. MemoryGame/CoreDataStore.swift:11
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. MemoryGame/GameView.swift:24
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. MemoryGame/Configuration.swift:8
Source import CoreData The cited file imports this module; runtime use and architectural role are not inferred. MemoryGame/CoreDataStore.swift:8
Source import os The cited file imports this module; runtime use and architectural role are not inferred. MemoryGame/CoreDataStore.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

MemoryGame/MemoryGameApp.swift:11 — representative type boundary

@main
struct MemoryGameApp: App {
    @StateObject private var store = CoreDataStore()
    // ...
}
Type Responsibility Depends on or conforms to
MemoryGameApp Application entry and top-level composition App
GameView User-interface presentation and input forwarding View
TileView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
CoreDataStore Centralized state or persistence access ObservableObject
DebugCoreDataView User-interface presentation and input forwarding View
DynamicFetchRequestView User-interface presentation and input forwarding Concrete collaborators/imported frameworks
MainMenuView User-interface presentation and input forwarding View
NewGameView User-interface presentation and input forwarding View
ProminentHighScoreView User-interface presentation and input forwarding 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
container (MemoryGame/CoreDataStore.swift:12) 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.
willSave (MemoryGame/GameEntity.swift:44) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
managedObjectContext (MemoryGame/GameView.swift:24) 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.
state (MemoryGame/GameView.swift:28) 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

MemoryGame/CoreDataStore.swift:12 — representative boundary

    private lazy var container: NSPersistentCloudKitContainer = {
        let container = NSPersistentCloudKitContainer(name: "MemoryGame")
        // ...
    }()

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 MemoryGameApp The source’s App suffix makes this role explicit.
Centralized state or persistence access CoreDataStore The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, DebugCoreDataView, DynamicFetchRequestView, GameView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Central store MemoryGame/CoreDataStore.swift:11 A store-named type centralizes feature state or persistence.

Naming conventions

  • Types: App: MemoryGameApp; Store: CoreDataStore; View: ContentView, DebugCoreDataView, DynamicFetchRequestView, GameView, MainMenuView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: cell, select, state, save, makePlayGameView, makeResetCurrentGamesView, resetCurrentGames.
  • Files: MemoryGame/MemoryGameApp.swift, MemoryGame/GameView.swift, MemoryGame/TileView.swift, MemoryGame/ContentView.swift, MemoryGame/CoreDataStore.swift, MemoryGame/DebugCoreDataView.swift.

Architecture takeaways

  • MemoryGameApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, CoreData 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
MemoryGame/MemoryGameApp.swift Cited implementation, MemoryGameApp
MemoryGame/CoreDataStore.swift Cited implementation, CoreDataStore, ObservableObject, CoreData, os
MemoryGame/GameEntity.swift Cited implementation, Feature implementation
MemoryGame/GameView.swift Cited implementation, DispatchQueue.main.asyncAfter, SwiftUI state property wrapper, GameView, SelectedState
MemoryGame/Configuration.swift SwiftUI, Configuration
MemoryGame/TileView.swift TileView, TileState
MemoryGame/ContentView.swift ContentView
MemoryGame/DebugCoreDataView.swift DebugCoreDataView
MemoryGame/DynamicFetchRequestView.swift DynamicFetchRequestView
MemoryGame/MainMenuView.swift MainMenuView
MemoryGame/NewGameView.swift NewGameView
MemoryGame/ProminentHighScoreView.swift ProminentHighScoreView
MemoryGame/SectionHeaderView.swift SectionHeaderView
MemoryGame/StandardHighScoreView.swift StandardHighScoreView
MemoryGame/DebugBorderViewModifier.swift DebugBorderViewModifier
MemoryGame/Sticker.swift Sticker