View in English

  • Apple 开发者
    • 入门汇总

    探索“入门汇总”

    • 概览
    • 学习
    • Apple Developer Program

    及时了解最新动态

    • 最新动态
    • 开发者你好
    • 平台

    探索“平台”

    • Apple 平台
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store

    精选

    • 设计
    • 分发
    • 游戏
    • 配件
    • 网页
    • Home
    • CarPlay 车载
    • 技术

    探索“技术”

    • 概览
    • Xcode
    • Swift
    • SwiftUI

    精选

    • 辅助功能
    • AI 与机器学习
    • App Intents
    • Apple 智能
    • 游戏
    • 安全性
    • Xcode Cloud
    • 社区

    探索“社区”

    • 概览
    • “与 Apple 会面交流”活动
    • 社区主导的活动
    • 开发者论坛
    • 开源

    精选

    • WWDC
    • Swift Student Challenge
    • 开发者故事
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Centers
    • 文档

    探索“文档”

    • 文档库
    • 技术概述
    • 示例代码
    • 《人机界面指南》
    • 视频

    发布说明

    • 精选更新
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • Apple tvOS
    • Xcode
    • 下载

    探索“下载”

    • 所有下载
    • 操作系统
    • 应用程序
    • 设计资源

    精选

    • Xcode
    • TestFlight
    • 字体
    • SF Symbols
    • Icon Composer
    • 支持

    探索“支持”

    • 概览
    • 帮助指南
    • 开发者论坛
    • “反馈助理”
    • 联系我们

    精选

    • 《开发者账户帮助》
    • 《App 审核指南》
    • 《App Store Connect 帮助》
    • 即将实行的要求
    • 协议和准则
    • 系统状态
  • 快速链接

    • 活动
    • 新闻
    • 论坛
    • 示例代码
    • 视频
 

视频

打开菜单 关闭菜单
  • 专题
  • 所有视频
  • 关于

更多视频

  • 简介
  • 概要
  • SwiftData 小组实验室

    参加我们的线上活动,与 Apple 工程师和设计师一起深入探索 WWDC26。在这个以 SwiftData 为主题的活动中,你可以提出问题、获取建议,并实时关注围绕大会一周的相关重磅发布展开的精彩讨论。活动语言为英语。

    章节

    • 0:00:00 - Introduction
    • 0:02:53 - What's the proper way to use SwiftData with widgets and App Intents (e.g. Shortcuts)? Is there Apple sample code showing this setup?
    • 0:04:48 - How should developers approach large datasets in SwiftData?
    • 0:07:42 - What's the best practice for creating sample data? Is inserting it via the #Preview traits parameter a good idea?
    • 0:09:55 - When should a developer choose ResultsObserver over @Query? What are the tradeoffs in performance, observation granularity, and architectural control?
    • 0:13:32 - In Core Data I used NSExpression to compute sums, averages, min/max directly on the SQL store; SwiftData has no equivalent and needs data in RAM. What's your recommendation?
    • 0:15:25 - When using SwiftUI with SwiftData, what's the best way to construct the view's state and model so users can populate non-optional attributes when adding a new object?
    • 0:18:53 - For SwiftData + CloudKit: if I published an app without an App Group or versioned schema, can I introduce them later without losing data?
    • 0:21:25 - How does using an App Group to define the SwiftData local store affect the CloudKit development and production environments, and how do I safely evolve the schema when multiple apps share the store?
    • 0:23:29 - What's the best way to manage schema evolution in development and production (CloudKit) for an app already on the App Store, when running a dev build in Xcode can cause the local store to stop syncing or duplicate entries?
    • 0:25:33 - What's the most performant way to count SwiftData objects without querying them into the views?
    • 0:25:47 - In performance-sensitive parts of an app I've manually fetched models into a cached array and managed updates myself instead of @Query. How does the team think about that tradeoff?
    • 0:30:20 - For SwiftData with CloudKit, how can I ensure synchronization happens as quickly as possible? I see long lags for data appearing across platforms.
    • 0:34:24 - What's the best way to store enums (including Codable ones) with associated values in SwiftData?
    • 0:41:09 - What are best practices for using multiple model contexts — when to split work across contexts vs. keeping everything in one?
    • 0:48:49 - How do I know if I need to paginate my queries instead of fetching all records of a type?
    • 0:50:20 - Is it a good idea to combine SwiftData with a cloud backend like Firestore for sync, or is that an anti-pattern? My app uses Firestore plus a local JSON mirror today.
    • 0:55:12 - What's the proper way to know if a database migration is required, so I can show a loading screen and keep widgets/App Intents from accessing the store mid-migration?
    • 0:57:33 - What's the best way to do grouped queries (grouping results by a model property) to display data section-wise?
    • 0:58:38 - How should I handle concurrency when a @ModelActor needs to return model data, given @Model types aren't Sendable and can't cross actor boundaries?

    资源

      • 高清视频
      • 标清视频
  • 搜索此视频…
    • 0:00:00 - Introduction
    • Engineers from the SwiftData team introduce themselves and set up a session covering SwiftData with widgets and app intents, large datasets and performance, @Query versus ResultsObserver, CloudKit sync and schema evolution, migrations, enums, multiple model contexts, and concurrency.

    • 0:02:53 - What's the proper way to use SwiftData with widgets and App Intents (e.g. Shortcuts)? Is there Apple sample code showing this setup?
    • In multi-process setups (app plus widget or App Intents extension), have one process — the app — own the database and perform migration, and coordinate access. Be careful if the user fires up the widget before launching the app for a new version, since the extension shouldn't be the one driving a migration. Design so the app owns schema changes and the extension reads once they're applied.

    • 0:04:48 - How should developers approach large datasets in SwiftData?
    • First define what "large" means for your case — many rows, large blobs stored per model, or simply many models — since the approach differs. For many rows: index the properties you query so the database can fetch them quickly, and don't fetch everything at once — put limits on your fetch requests so you don't exhaust memory. For large per-model data, use the externalStorage attribute, which stores the blob in a file next to the database rather than inline. When ingesting a large dataset, insert in smaller batches using a short-lived model context you renew each batch. And when driving a @Query in SwiftUI over many rows, make sure a precise predicate expresses exactly what you need (rather than filtering again with an if in the body) and that it's covered by an index; the persistence Instrument shows how many objects are actually being fetched under the hood.

    • 0:07:42 - What's the best practice for creating sample data? Is inserting it via the #Preview traits parameter a good idea?
    • Yes — the Sample Trips app uses preview traits to seed preview data, which is a great approach. Make the sample data expressive and diverse (long names, many entries, varied cases) so your previews exercise the full range of UI states rather than a single trivial example.

    • 0:09:55 - When should a developer choose ResultsObserver over @Query? What are the tradeoffs in performance, observation granularity, and architectural control?
    • ResultsObserver is the equivalent of @Query for use outside a SwiftUI view — reach for it in a view model or other non-view context where you still want observable results. @Query is the right tool inside views. Choose based on where the code lives and how much architectural control you want over observation, rather than a raw performance difference.

    • 0:13:32 - In Core Data I used NSExpression to compute sums, averages, min/max directly on the SQL store; SwiftData has no equivalent and needs data in RAM. What's your recommendation?
    • This is a known gap — the older NSExpression surface is being succeeded by newer Swift-based expression support, and it isn't all there yet, so a feedback request with your specific use case is genuinely useful for shaping the API. In the meantime there are workarounds: you can get min or max with a fetch limit of one plus a sort descriptor. And because Core Data and SwiftData can coexist against the same data store, where SwiftData lacks an equivalent you can reach into the Core Data stack and use NSExpression there on the same underlying data.

    • 0:15:25 - When using SwiftUI with SwiftData, what's the best way to construct the view's state and model so users can populate non-optional attributes when adding a new object?
    • Distinguish non-optional view state from non-optional model properties. A common pattern is to collect the user's input in view-local state first and only construct/insert the SwiftData model once the required (non-optional) values are provided, rather than inserting a partially-initialized object. That keeps the model's non-optional invariants intact while the form is being filled in.

    • 0:18:53 - For SwiftData + CloudKit: if I published an app without an App Group or versioned schema, can I introduce them later without losing data?
    • You can add a versioned schema at any time. Moving to an App Group container is more involved: it's a different directory and entitlements can't be aligned to the old location, so you'll get a new container and must copy the existing data over into the group container, then start from there. Set up your model configuration accordingly before switching.

    • 0:21:25 - How does using an App Group to define the SwiftData local store affect the CloudKit development and production environments, and how do I safely evolve the schema when multiple apps share the store?
    • Ensure every app reading from the App Group uses the same, appropriate CloudKit entitlement, since all of them sync to that shared container on the store's behalf. With consistent entitlements and a shared versioned schema across those apps, you can evolve the schema safely; mismatched entitlements or schemas across the group cause sync problems.

    • 0:23:29 - What's the best way to manage schema evolution in development and production (CloudKit) for an app already on the App Store, when running a dev build in Xcode can cause the local store to stop syncing or duplicate entries?
    • When a development build and the shipping app write to the same shared CloudKit store while the schema evolves, you must guard against duplicates. Keep development and production schemas coordinated through versioned schemas, and be deliberate about which clients write to the shared container so an evolving dev schema doesn't corrupt or duplicate production data.

    • 0:25:33 - What's the most performant way to count SwiftData objects without querying them into the views?
    • Use fetchCount on the model context — it returns the count without materializing the objects, which is far cheaper than fetching them and counting in the view.

    • 0:25:47 - In performance-sensitive parts of an app I've manually fetched models into a cached array and managed updates myself instead of @Query. How does the team think about that tradeoff?
    • @Query does a normal fetch under the hood; the tricky part in SwiftUI is that the view cycle can fetch more often than you'd like. Manually fetching into a cached array and managing updates is a legitimate optimization for hot paths where you need tight control, as long as you handle observation yourself — it's a reasonable tradeoff, not an anti-pattern, when @Query's automatic re-fetching is costing you.

    • 0:30:20 - For SwiftData with CloudKit, how can I ensure synchronization happens as quickly as possible? I see long lags for data appearing across platforms.
    • Beyond thorough testing, use the extensive logging that the default Core Data-backed store routes through to see what sync is doing and where time goes. CloudKit sync timing isn't fully under your control, but diagnosing via those logs — and ensuring the right client drives sync — helps you understand and reduce the cross-device lag.

    • 0:34:24 - What's the best way to store enums (including Codable ones) with associated values in SwiftData?
    • As long as the associated values are Codable, you can persist the enum. New this year, you can build predicates with RawRepresentable enumerations — covered in the What's New in SwiftData talk — so enums integrate more naturally into queries as well as storage.

    • 0:41:09 - What are best practices for using multiple model contexts — when to split work across contexts vs. keeping everything in one?
    • It depends on your objective. If the work interacts with a view, that shapes the choice; but for a self-contained background work set, a separate context (e.g. via a ModelActor) is appropriate so heavy work doesn't block the main context. Keep view-driving work on the main context and isolate background batches in their own context, merging results back deliberately.

    • 0:48:49 - How do I know if I need to paginate my queries instead of fetching all records of a type?
    • Let Instruments tell you. Watch for UI hitches and check whether you're pulling in many more objects than you actually display — if so, paginate or limit the fetch. Use FetchDescriptor's fetch limits and offsets to load in pages sized to what the UI shows, rather than fetching an entire type up front.

    • 0:50:20 - Is it a good idea to combine SwiftData with a cloud backend like Firestore for sync, or is that an anti-pattern? My app uses Firestore plus a local JSON mirror today.
    • It's a valid approach, not an anti-pattern. If your data already lives in JSON files, you can implement a custom SwiftData store backed by that source — there's an example app demonstrating a custom store — letting SwiftData manage local access while your existing backend continues to handle sync.

    • 0:55:12 - What's the proper way to know if a database migration is required, so I can show a loading screen and keep widgets/App Intents from accessing the store mid-migration?
    • For a progress UI, you can compute how many migration stages exist and override the didMigrate handler for a custom migration stage, tracking which stage you're on versus how many remain — not wall-clock time, but a stage count. Combine that with having the app own migration so extensions don't touch the store while it's mid-migration.

    • 0:57:33 - What's the best way to do grouped queries (grouping results by a model property) to display data section-wise?
    • Both ResultsObserver and @Query now support sectioning: provide a key path to a persistent property and your data is grouped by it. This gives a built-in group-by experience for section-based UIs (demonstrated in the Sample Trips app) without manually bucketing results yourself.

    • 0:58:38 - How should I handle concurrency when a @ModelActor needs to return model data, given @Model types aren't Sendable and can't cross actor boundaries?
    • Model objects aren't Sendable — they're reference-based and part of the context's object graph, so they can't be passed across actor boundaries. Instead, have the ModelActor return a Sendable representation: the model's PersistentIdentifier (or a plain value/DTO) that the receiving actor uses to re-fetch the object in its own context, rather than handing over the model instance directly.

开发者页脚

  • 视频
  • WWDC26
  • SwiftData 小组实验室
  • 打开菜单 关闭菜单
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store
    打开菜单 关闭菜单
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    打开菜单 关闭菜单
    • 辅助功能
    • 配件
    • AI 与机器学习
    • Apple 智能
    • 音频与视频
    • 增强现实
    • 商务
    • 设计
    • 分发
    • 教育
    • 游戏
    • 健康与健身
    • App 内购买项目
    • 本地化
    • 地图与位置
    • 安全性
    • Safari 浏览器与网页
    打开菜单 关闭菜单
    • 文档
    • 下载
    • 示例代码
    • 视频
    • 文档归档
    打开菜单 关闭菜单
    • 帮助指南与文章
    • 联系我们
    • 论坛
    • 反馈与错误报告
    • 系统状态
    打开菜单 关闭菜单
    • Apple 开发者
    • App Store Connect
    • 证书、标识符和描述文件
    • “反馈助理”
    打开菜单 关闭菜单
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • 安全赏金计划
    • Security Research Device Program
    打开菜单 关闭菜单
    • 与 Apple 会面交流
    • Apple Developer Center
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Academy
    • WWDC
    阅读最近新闻。
    获取 Apple Developer App。
    版权所有 © 2026 Apple Inc. 保留所有权利。
    使用条款 隐私政策 协议和准则