Restoring your app’s state with AppKit
At a glance
| Item | Summary |
|---|---|
| Purpose | Provide continuity for people using your app by preserving current activities on macOS. |
| App architecture | A Swift sample with the source-visible chain AppDelegate → MainWindowController → DataModelManager → WindowRestorationHandler → AppKit APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks |
| Project style | 14 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Sendable or @Sendable; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: NotificationCenter. |
| Key frameworks/packages | AppKit, Foundation; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── StateRestoration/
└── StateRestoration/
├── AppDelegate.swift
├── Main Window/
│ ├── EditViewController.swift
│ ├── DetailContainerViewController.swift
│ ├── MainWindowController.swift
│ ├── CollectionViewController.swift
│ ├── InfoViewController.swift
│ └── MainSplitViewController.swift
├── Model/
│ ├── DataModel.swift
│ └── Product.swift
├── Image Window/
│ ├── ImageViewController.swift
│ └── ImageWindowController.swift
└── RestorationConstants.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 5 project/configuration file(s) and 21 source declaration(s).
Overall architecture
flowchart LR
N1["AppDelegate"]
N2["MainWindowController"]
N3["DataModelManager"]
N4["WindowRestorationHandler"]
N5["AppKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
StateRestoration/StateRestoration/AppDelegate.swift:10 — architecture anchor
@main @MainActor
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
var mainWindowController: MainWindowController?
// ...
}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 AppKit.
Ownership and state
classDiagram
AppDelegate o-- MainWindowController : mainWindowController
AppDelegate *-- Array : imageWindowControllers
EditorState *-- String : title
EditorState *-- String : price
Ownership evidence
StateRestoration/StateRestoration/AppDelegate.swift:13 — stored dependency or nearest verified ownership anchor
@main @MainActor
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
var mainWindowController: MainWindowController?
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AppDelegate |
MainWindowController (mainWindowController) |
stores or receives | App/module collaborators |
AppDelegate |
Array (imageWindowControllers) |
owns value state | App/module collaborators |
EditorState |
String (title) |
owns value state | Initialized by the owner; the binding is immutable |
EditorState |
String (price) |
owns value state | 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 |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | StateRestoration/StateRestoration/AppDelegate.swift:10 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | StateRestoration/StateRestoration/Model/DataModel.swift:102 |
@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
StateRestoration/StateRestoration/AppDelegate.swift:10 — representative execution boundary
@main @MainActor
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
var mainWindowController: MainWindowController?
// ...
}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 | NotificationCenter |
NotificationCenter distributes named process-local events. | StateRestoration/StateRestoration/Image Window/ImageWindowController.swift:50 |
| Source import | AppKit |
The cited file imports this module; runtime use and architectural role are not inferred. | StateRestoration/StateRestoration/AppDelegate.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | StateRestoration/StateRestoration/Model/DataModel.swift:8 |
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
StateRestoration/StateRestoration/Main Window/EditViewController.swift:12 — representative type boundary
@MainActor protocol EditViewControllerDelegate: AnyObject {
func didFinishEditing(product: Product)
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
EditViewControllerDelegate |
Defines a capability or collaboration contract | AnyObject |
EditViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
DataModelManager |
Long-lived feature or framework coordination | Concrete collaborators/imported frameworks |
DataModel |
Feature data or observable state | Codable, Sendable |
DetailContainerViewController |
View lifecycle, callbacks, and feature coordination | NSTabViewController |
MainWindowController |
View lifecycle, callbacks, and feature coordination | NSWindowController, NSWindowDelegate |
ImageViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
ImageWindowController |
View lifecycle, callbacks, and feature coordination | NSWindowController, NSWindowDelegate |
CollectionViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
The source explicitly defines local protocol relationships: DetailContainerViewController → EditViewControllerDelegate.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
productImageView (StateRestoration/StateRestoration/Image Window/ImageViewController.swift:13) |
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. |
updateUI (StateRestoration/StateRestoration/Image Window/ImageViewController.swift:51) |
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. |
imageViewController (StateRestoration/StateRestoration/Image Window/ImageWindowController.swift:13) |
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. |
product (StateRestoration/StateRestoration/Image Window/ImageWindowController.swift:14) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
Reference code
StateRestoration/StateRestoration/Image Window/ImageViewController.swift:13 — representative boundary
@MainActor
class ImageViewController: NSViewController {
// ...
private var productImageView: NSImageView!
// ...
}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 |
|---|---|---|
| View lifecycle, callbacks, and feature coordination | CollectionViewController, DetailContainerViewController, EditViewController, ImageViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, EditViewControllerDelegate |
The source’s Delegate suffix makes this role explicit. |
| Handles callbacks or feature events | WindowRestorationHandler |
The source’s Handler suffix makes this role explicit. |
| Long-lived feature or framework coordination | DataModelManager |
The source’s Manager suffix makes this role explicit. |
| Feature data or observable state | DataModel |
The source’s Model suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | StateRestoration/StateRestoration/Main Window/CollectionViewController.swift:11 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | StateRestoration/StateRestoration/Main Window/DetailContainerViewController.swift:115 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | StateRestoration/StateRestoration/AppDelegate.swift:11 |
Callback protocols invert event delivery back into the sample’s owner. |
Naming conventions
- Types: Controller: CollectionViewController, DetailContainerViewController, EditViewController, ImageViewController, ImageWindowController; Delegate: AppDelegate, EditViewControllerDelegate; Handler: WindowRestorationHandler; Manager: DataModelManager; Model: DataModel.
- Protocols:
EditViewControllerDelegate. - Methods:
applicationDidFinishLaunching,applicationWillTerminate,applicationShouldTerminateAfterLastWindowClosed,applicationSupportsSecureRestorableState,openImageWindow,removeImageWindowController,didFinishEditing,loadView. - Files:
StateRestoration/StateRestoration/AppDelegate.swift,StateRestoration/StateRestoration/Main Window/EditViewController.swift,StateRestoration/StateRestoration/Model/DataModel.swift,StateRestoration/StateRestoration/Main Window/DetailContainerViewController.swift,StateRestoration/StateRestoration/Main Window/MainWindowController.swift,StateRestoration/StateRestoration/Image Window/ImageViewController.swift.
Architecture takeaways
AppDelegateis the main source-visible entry or composition anchor for this sample.- Framework work reaches AppKit 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
StateRestoration/StateRestoration/AppDelegate.swift |
Cited implementation, @MainActor, AppKit, AppDelegate |
StateRestoration/StateRestoration/Main Window/EditViewController.swift |
EditViewControllerDelegate, EditorState, EditViewController |
StateRestoration/StateRestoration/Image Window/ImageViewController.swift |
Cited implementation, ImageViewController |
StateRestoration/StateRestoration/Image Window/ImageWindowController.swift |
Cited implementation, NotificationCenter, ImageWindowController |
StateRestoration/StateRestoration/Main Window/CollectionViewController.swift |
CollectionViewController |
StateRestoration/StateRestoration/Main Window/DetailContainerViewController.swift |
Cited implementation, DetailContainerViewController, EditorRestoreState |
StateRestoration/StateRestoration/Model/DataModel.swift |
Sendable or @Sendable, Foundation, DataModelManager, DataModel, CodingKeys |
StateRestoration/StateRestoration/Main Window/MainWindowController.swift |
MainWindowController, ToolbarItemIdentifier |
StateRestoration/StateRestoration/Main Window/InfoViewController.swift |
InfoViewController |
StateRestoration/StateRestoration/Main Window/MainSplitViewController.swift |
MainSplitViewController |
StateRestoration/StateRestoration/Model/Product.swift |
Product, CoderKeys |
StateRestoration/StateRestoration/RestorationConstants.swift |
RestorationKeys, WindowIdentifiers |
StateRestoration/StateRestoration/Main Window/CollectionViewItem.swift |
CollectionViewItem |
StateRestoration/StateRestoration/WindowRestoration.swift |
WindowRestorationHandler |