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
  • Visualize and optimize Swift concurrency

    Learn how you can optimize your app with the Swift Concurrency template in Instruments. We'll discuss common performance issues and show you how to use Instruments to find and resolve these problems. Learn how you can keep your UI responsive, maximize parallel performance, and analyze Swift concurrency activity within your app.

    To get the most out of this session, we recommend familiarity with Swift concurrency (including tasks and actors).

    Ressources

    • Concurrency
    • The Swift Programming Language: Concurrency
      • Vidéo HD
      • Vidéo SD

    Vidéos connexes

    WWDC23

    • Analyze hangs with Instruments
    • Beyond the basics of structured concurrency

    WWDC22

    • Eliminate data races using Swift Concurrency
    • Track down hangs with Xcode and on-device detection
    • What's new in Swift
    • What's new in UIKit

    WWDC21

    • Explore structured concurrency in Swift
    • Meet async/await in Swift
    • Protect mutable state with Swift actors
    • Swift concurrency: Behind the scenes
    • Swift concurrency: Update a sample app
  • Rechercher dans cette vidéo…
    • 10:24 - CompressionState class

      @MainActor
      class CompressionState: ObservableObject {
          @Published var files: [FileStatus] = []
          var logs: [String] = []
          
          func update(url: URL, progress: Double) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].progress = progress
              }
          }
          
          func update(url: URL, uncompressedSize: Int) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].uncompressedSize = uncompressedSize
              }
          }
          
          func update(url: URL, compressedSize: Int) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].compressedSize = compressedSize
              }
          }
          
          func compressAllFiles() {
              for file in files {
                  Task {
                      let compressedData = compressFile(url: file.url)
                      await save(compressedData, to: file.url)
                  }
              }
          }
          
          func compressFile(url: URL) -> Data {
              log(update: "Starting for \(url)")
              let compressedData = CompressionUtils.compressDataInFile(at: url) { uncompressedSize in
                  update(url: url, uncompressedSize: uncompressedSize)
              } progressNotification: { progress in
                  update(url: url, progress: progress)
                  log(update: "Progress for \(url): \(progress)")
              } finalNotificaton: { compressedSize in
                  update(url: url, compressedSize: compressedSize)
              }
              log(update: "Ending for \(url)")
              return compressedData
          }
          
          func log(update: String) {
              logs.append(update)
          }
    • 11:49 - CompressionState class using ParallelCompressor actor

      actor ParallelCompressor {
          var logs: [String] = []
          unowned let status: CompressionState
          
          init(status: CompressionState) {
              self.status = status
          }
          
          func compressFile(url: URL) -> Data {
              log(update: "Starting for \(url)")
              let compressedData = CompressionUtils.compressDataInFile(at: url) { uncompressedSize in
                  Task { @MainActor in
                      status.update(url: url, uncompressedSize: uncompressedSize)
                  }
              } progressNotification: { progress in
                  Task { @MainActor in
                      status.update(url: url, progress: progress)
                      await log(update: "Progress for \(url): \(progress)")
                  }
              } finalNotificaton: { compressedSize in
                  Task { @MainActor in
                      status.update(url: url, compressedSize: compressedSize)
                  }
              }
              log(update: "Ending for \(url)")
              return compressedData
          }
          
          func log(update: String) {
              logs.append(update)
          }
      }
      
      @MainActor
      class CompressionState: ObservableObject {
          @Published var files: [FileStatus] = []
          var compressor: ParallelCompressor!
          
          init() {
              self.compressor = ParallelCompressor(status: self)
          }
          
          func update(url: URL, progress: Double) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].progress = progress
              }
          }
          
          func update(url: URL, uncompressedSize: Int) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].uncompressedSize = uncompressedSize
              }
          }
          
          func update(url: URL, compressedSize: Int) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].compressedSize = compressedSize
              }
          }
          
          func compressAllFiles() {
              for file in files {
                  Task {
                      let compressedData = await compressor.compressFile(url: file.url)
                      await save(compressedData, to: file.url)
                  }
              }
          }
      }
    • 17:46 - CompressionState class using ParallelCompressor with minimal actor-isolation and detached tasks

      actor ParallelCompressor {
          var logs: [String] = []
          unowned let status: CompressionState
          
          init(status: CompressionState) {
              self.status = status
          }
          
          nonisolated func compressFile(url: URL) async -> Data {
              await log(update: "Starting for \(url)")
              let compressedData = CompressionUtils.compressDataInFile(at: url) { uncompressedSize in
                  Task { @MainActor in
                      status.update(url: url, uncompressedSize: uncompressedSize)
                  }
              } progressNotification: { progress in
                  Task { @MainActor in
                      status.update(url: url, progress: progress)
                      await log(update: "Progress for \(url): \(progress)")
                  }
              } finalNotificaton: { compressedSize in
                  Task { @MainActor in
                      status.update(url: url, compressedSize: compressedSize)
                  }
              }
              await log(update: "Ending for \(url)")
              return compressedData
          }
          
          func log(update: String) {
              logs.append(update)
          }
      }
      
      @MainActor
      class CompressionState: ObservableObject {
          @Published var files: [FileStatus] = []
          var compressor: ParallelCompressor!
          
          init() {
              self.compressor = ParallelCompressor(status: self)
          }
          
          func update(url: URL, progress: Double) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].progress = progress
              }
          }
          
          func update(url: URL, uncompressedSize: Int) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].uncompressedSize = uncompressedSize
              }
          }
          
          func update(url: URL, compressedSize: Int) {
              if let loc = files.firstIndex(where: {$0.url == url}) {
                  files[loc].compressedSize = compressedSize
              }
          }
          
          func compressAllFiles() {
              for file in files {
                  Task.detached {
                      let compressedData = await self.compressor.compressFile(url: file.url)
                      await save(compressedData, to: file.url)
                  }
              }
          }
      }

Developer Footer

  • Vidéos
  • WWDC22
  • Visualize and optimize Swift concurrency
  • 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