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 MemoryGameApp → ContentView → CoreDataStore → SwiftUI / CoreData APIs. |
| Main patterns | Central store |
| Project style | 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
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
flowchart LR
N1["MemoryGameApp"]
N2["ContentView"]
N3["CoreDataStore"]
N4["SwiftUI / CoreData APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
MemoryGame/MemoryGameApp.swift:10 — architecture anchor
@main
struct MemoryGameApp: App {
// ...
}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
classDiagram
MemoryGameApp *-- CoreDataStore : store
GameView o-- GameEntity : game
TileView *-- String : sticker
TileView o-- TileState : state
Ownership evidence
MemoryGame/MemoryGameApp.swift:12 — stored dependency or nearest verified ownership anchor
@StateObject private var store = CoreDataStore()
var body: some Scene {
WindowGroup {
ContentView().environment(\.managedObjectContext, store.viewContext)
}
}| 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.
Class and protocol design
MemoryGame/MemoryGameApp.swift:11 — representative type boundary
struct MemoryGameApp: App {
// ...
}| 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 = {
// ...
}()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
MemoryGameAppis 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 |
MemoryGameApp |
MemoryGame/GameView.swift |
GameView, SelectedState |
MemoryGame/TileView.swift |
TileView, TileState |
MemoryGame/ContentView.swift |
ContentView |
MemoryGame/CoreDataStore.swift |
CoreDataStore |
MemoryGame/DebugCoreDataView.swift |
DebugCoreDataView |
MemoryGame/DynamicFetchRequestView.swift |
DynamicFetchRequestView |
MemoryGame/MainMenuView.swift |
MainMenuView |
MemoryGame/NewGameView.swift |
NewGameView |
MemoryGame/ProminentHighScoreView.swift |
ProminentHighScoreView |