swiftui-patterns

Structure SwiftUI view hierarchies and state ownership for iOS 26+ apps.

Updated Mar 31, 2026
One-click install
npx skills add https://github.com/DFly7/iOS-FastAPI-Supabase-AI --skill swiftui-patterns-dfly7
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swiftui-patterns
Source: https://github.com/DFly7/iOS-FastAPI-Supabase-AI/tree/main/.agents/skills/swiftui-patterns
Command: npx skills add https://github.com/DFly7/iOS-FastAPI-Supabase-AI --skill swiftui-patterns-dfly7

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides concrete guidance to structure SwiftUI views, manage state ownership, and compose view hierarchies so apps remain performant, testable, and easy to maintain across iOS 26+ and Swift 6.3. It reduces ambiguity about when views should own state versus when shared models belong in the environment, and it prescribes modern async loading and lifecycle patterns to avoid common UI bugs and performance regressions.

Core Features & Use Cases

  • Architecture guidance: Default Model-View (MV) pattern, app wiring, dependency graph modifier, and lightweight client patterns for network and service layers.
  • State management rules: Ownership conventions for @State, @Bindable, let, @Environment, and @Observable with explicit @MainActor guidance.
  • View composition & performance: Decompose large views, use Lazy stacks/grids, prefer stable Identifiable IDs, and create custom ViewModifiers for repeated styles.
  • Async & lifecycle: Use .task and .task(id:) for cancellable async loading, prefer .sheet(item:) for modal presentation, and avoid manual Task creation in onAppear.
  • Migration & review: Migration mappings from older APIs (ObservableObject, NavigationView) and a review checklist to validate HIG alignment, accessibility, and stability.
  • Use case example: Implement a Feed screen that injects a client from the environment, loads posts with .task, displays results in a lazy list, and follows @Observable ownership rules.

Quick Start

Create a SwiftUI FeedView targeting iOS 26 that uses the MV pattern, injects a FeedClient via the environment, loads posts in .task, displays them with a LazyVStack or List using stable Identifiable IDs, and ensures @Observable view models are @MainActor-isolated.

Frequently Asked Questions about swiftui-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage SwiftUI state ownership between @State, @Bindable, and @Environment?

SwiftUI state ownership requires using @State for local view state, @Bindable for binding to @Observable models, and @Environment for shared dependency injection. This separation ensures apps remain testable and performant across iOS 26+ and Swift 6.3.

What is the best way to load async data in SwiftUI views without causing UI bugs?

The best way to load async data in SwiftUI is using .task or .task(id:) modifiers for cancellable lifecycle-driven loading. Avoid manual Task creation in onAppear to prevent UI bugs and ensure proper cancellation when views disappear.

How do I structure SwiftUI views using the Model-View pattern with @Observable?

Structure SwiftUI views using the Model-View pattern by applying @MainActor isolation to @Observable view models, injecting lightweight clients via the environment, and decomposing large view hierarchies into composable, testable components.

How do I improve SwiftUI list performance for large collections?

Improve SwiftUI list performance for large collections by using LazyVStack or List with stable Identifiable IDs. Lazy stacks defer view creation until needed, preventing performance regressions when rendering extensive data sets.

How do I migrate older SwiftUI APIs like ObservableObject and NavigationView?

Migrate older SwiftUI APIs by mapping ObservableObject to @Observable and replacing NavigationView with modern navigation containers. Apply review checklists to validate HIG alignment, accessibility, and stability after migration.

When should I use @Environment for dependency injection in SwiftUI?

Use @Environment for dependency injection in SwiftUI when sharing services like network clients across multiple views. This environment-based approach enables testable clients and decouples view models from concrete dependencies.