View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vidéos

Ouvrir le menu Fermer le menu
  • Collections
  • Toutes les vidéos
  • À propos

Plus de vidéos

  • À propos
  • Code
  • Dive deeper into SwiftData

    Learn how you can harness the power of SwiftData in your app. Find out how ModelContext and ModelContainer work together to persist your app's data. We'll show you how to track and make your changes manually and use SwiftData at scale with FetchDescriptor, SortDescriptor, and enumerate.

    To get the most out of this session, we recommend first watching "Meet SwiftData" and "Model your schema with SwiftData" from WWDC23.

    Chapitres

    • 0:00 - Intro
    • 3:42 - Configuring persistence
    • 7:21 - Track and persist changes
    • 11:20 - Modeling at scale
    • 14:54 - Wrap-up

    Ressources

    • SwiftData
    • Adopting SwiftData for a Core Data app
      • Vidéo HD
      • Vidéo SD

    Vidéos connexes

    WWDC23

    • Build an app with SwiftData
    • Discover Observation in SwiftUI
    • Meet SwiftData
    • Migrate to SwiftData
    • Model your schema with SwiftData
  • Rechercher dans cette vidéo…
    • 1:45 - Trip model with cascading relationships

      @Model
      final class Trip {
          var destination: String?
          var end_date: Date?
          var name: String?
          var start_date: Date?
        
          @Relationship(.cascade)
          var bucketListItem: [BucketListItem] = [BucketListItem]()
        
          @Relationship(.cascade)
          var livingAccommodation: LivingAccommodation?
      }
    • 4:21 - Initializing a ModelContainer

      // ModelContainer initialized with just Trip
      let container = try ModelContainer(for: Trip.self)
      
      // SwiftData infers related model classes as well
      let container = try ModelContainer(
          for: [
              Trip.self, 
              BucketListItem.self, 
              LivingAccommodation.self
          ]
      )
    • 5:41 - Using ModelConfiguration to customize ModelContainer

      let fullSchema = Schema([
          Trip.self,
          BucketListItem.self,
          LivingAccommodations.self,
          Person.self,
          Address.self
      ])
      
      let trips = ModelConfiguration(
          schema: Schema([
              Trip.self,
              BucketListItem.self,
              LivingAccommodations.self
          ]),
          url: URL(filePath: "/path/to/trip.store"),
          cloudKitContainerIdentifier: "com.example.trips"
      )
      
      let people = ModelConfiguration(
          schema: Schema([Person.self, Address.self]),
          url: URL(filePath: "/path/to/people.store"),
          cloudKitContainerIdentifier: "com.example.people"
      ) 
      
      let container = try ModelContainer(for: fullSchema, trips, people)
    • 6:49 - Creating ModelContainer in SwiftUI

      @main
      struct TripsApp: App {
          let fullSchema = Schema([
              Trip.self, 
              BucketListItem.self,
              LivingAccommodations.self,
              Person.self, 
              Address.self
          ])
        
          let trips = ModelConfiguration(
              schema: Schema([
                  Trip.self,
                  BucketListItem.self,
                  LivingAccommodations.self
              ]),
              url: URL(filePath: "/path/to/trip.store"),
              cloudKitContainerIdentifier: "com.example.trips"
          )
        
          let people = ModelConfiguration(
              schema: Schema([
                  Person.self, 
                  Address.self
              ]),
              url: URL(filePath: "/path/to/people.store"),
              cloudKitContainerIdentifier: "com.example.people"
          )
        
          let container = try ModelContainer(for: fullSchema, trips, people)
          var body: some Scene {
              WindowGroup {
                  ContentView()
              }
              .modelContainer(container)
          }
      }
    • 7:40 - Using the modelContainer modifier

      @main
      struct TripsApp: App {
         var body: some Scene {
              WindowGroup {
                  ContentView()
              }
              .modelContainer(for: Trip.self)
          }
      }
    • 7:50 - Referencing a ModelContext in SwiftUI views

      struct ContentView: View {
          @Query var trips: [Trip]
          @Environment(\.modelContext) var modelContext
        
          var body: some View {
              NavigationStack (path: $path) {
                  List(selection: $selection) {
                      ForEach(trips) { trip in
                          TripListItem(trip: trip)
                              .swipeActions(edge: .trailing) {
                                  Button(role: .destructive) {
                                      modelContext.delete(trip)
                                  } label: {
                                      Label("Delete", systemImage: "trash")
                                  }
                              }
                      }
                      .onDelete(perform: deleteTrips(at:))
                  }
              }
          }
      }
    • 9:57 - Enabling undo on a ModelContainer

      @main
      struct TripsApp: App {
         @Environment(\.undoManager) var undoManager
         var body: some Scene {
              WindowGroup {
                  ContentView()
              }
              .modelContainer(for: Trip.self, isUndoEnabled: true)
          }
      }
    • 11:05 - Enabling autosave on a ModelContainer

      @main
      struct TripsApp: App {
         var body: some Scene {
              WindowGroup {
                  ContentView()
              }
              .modelContainer(for: Trip.self, isAutosaveEnabled: false)
          }
      }
    • 11:54 - Fetching objects with FetchDescriptor

      let context = self.newSwiftContext(from: Trip.self)
      var trips = try context.fetch(FetchDescriptor<Trip>())
    • 12:14 - Fetching objects with #Predicate and FetchDescriptor

      let context = self.newSwiftContext(from: Trip.self)
      let hotelNames = ["First", "Second", "Third"]
      
      var predicate = #Predicate<Trip> { trip in
          trip.livingAccommodations.filter {
              hotelNames.contains($0.placeName)
          }.count > 0
      }
      
      var descriptor = FetchDescriptor(predicate: predicate)
      var trips = try context.fetch(descriptor)
    • 12:27 - Fetching objects with #Predicate and FetchDescriptor

      let context = self.newSwiftContext(from: Trip.self)
      
      predicate = #Predicate<Trip> { trip in
          trip.livingAccommodations.filter {
              $0.hasReservation == false
          }.count > 0
      }
      
      descriptor = FetchDescriptor(predicate: predicate)
      var trips = try context.fetch(descriptor)
    • 13:18 - Enumerating objects with FetchDescriptor

      context.enumerate(FetchDescriptor<Trip>()) { trip in
          // Operate on trip
      }
    • 13:36 - Enumerating with FetchDescriptor and SortDescriptor

      let predicate = #Predicate<Trip> { trip in
          trip.bucketListItem.filter {
              $0.hasReservation == false
          }.count > 0
      }
      
      let descriptor = FetchDescriptor(predicate: predicate)
      descriptor.sortBy = [SortDescriptor(\.start_date)]
      
      context.enumerate(descriptor) { trip in
          // Remind me to make reservations for trip
      }
    • 14:01 - Fine tuning enumerate with batchSize

      let predicate = #Predicate<Trip> { trip in
          trip.bucketListItem.filter {
              $0.hasReservation == false
          }.count > 0
      }
      
      let descriptor = FetchDescriptor(predicate: predicate)
      descriptor.sortBy = [SortDescriptor(\.start_date)]
      
      context.enumerate(
          descriptor,
          batchSize: 10000
      ) { trip in
          // Remind me to make reservations for trip
      }
    • 14:28 - Fine tuning enumerate with batchSize and allowEscapingMutations

      let predicate = #Predicate<Trip> { trip in
          trip.bucketListItem.filter {
              $0.hasReservation == false
          }.count > 0
      }
      
      let descriptor = FetchDescriptor(predicate: predicate)
      descriptor.sortBy = [SortDescriptor(\.start_date)]
      
      context.enumerate(
          descriptor,
          batchSize: 500,
          allowEscapingMutations: true
      ) { trip in
          // Remind me to make reservations for trip
      }

Developer Footer

  • Vidéos
  • WWDC23
  • Dive deeper into SwiftData
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • 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 Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines