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
  • Meet AsyncSequence

    Iterating over a sequence of values over time is now as easy as writing a “for” loop. Find out how the new AsyncSequence protocol enables a natural, simple syntax for iterating over anything from notifications to bytes being streamed from a server. We'll also show you how to adapt existing code to provide asynchronous sequences of your own.

    To get the most out of this session, we recommend first watching “Meet async/await in Swift.”

    Ressources

    • SE-0314: AsyncStream and AsyncThrowingStream
    • SE-0298: Async/Await: Sequences
    • The Swift Programming Language: Concurrency
      • Vidéo HD
      • Vidéo SD

    Vidéos connexes

    WWDC22

    • Compose advanced models with Create ML Components
    • Create a more responsive media app
    • Meet Swift Async Algorithms

    WWDC21

    • Explore structured concurrency in Swift
    • Meet async/await in Swift
    • Use async/await with URLSession
    • What's new in Foundation
  • Rechercher dans cette vidéo…
    • 0:37 - QuakesTool

      @main
      struct QuakesTool {
          static func main() async throws {
              let endpointURL = URL(string: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv")!
      
              // skip the header line and iterate each one 
              // to extract the magnitude, time, latitude and longitude
              for try await event in endpointURL.lines.dropFirst() {
                  let values = event.split(separator: ",")
                  let time = values[0]
                  let latitude = values[1]
                  let longitude = values[2]
                  let magnitude = values[4]
                  print("Magnitude \(magnitude) on \(time) at \(latitude) \(longitude)")
              }
          }
      }
    • 3:24 - Iterating a Sequence

      for quake in quakes {
          if quake.magnitude > 3 {
              displaySignificantEarthquake(quake)
          }
      }
    • 3:52 - How the compiler handles iteration

      var iterator = quakes.makeIterator()
      while let quake = iterator.next() {
          if quake.magnitude > 3 {
              displaySignificantEarthquake(quake)
          }
      }
    • 4:11 - How the compiler handles asynchronous iteration

      var iterator = quakes.makeAsyncIterator()
      while let quake = await iterator.next() {
          if quake.magnitude > 3 {
              displaySignificantEarthquake(quake)
          }
      }
    • 4:28 - Iterating an AsyncSequence

      for await quake in quakes {
          if quake.magnitude > 3 {
              displaySignificantEarthquake(quake)
          }
      }
    • 5:36 - Terminating iteration early by breaking

      for await quake in quakes {
          if quake.location == nil {
              break
          }
          if quake.magnitude > 3 {
              displaySignificantEarthquake(quake)
          }
      }
    • 5:51 - Skipping values by continuing

      for await quake in quakes {
          if quake.depth > 5 {
              continue
          }
          if quake.magnitude > 3 {
              displaySignificantEarthquake(quake)
          }
      }
    • 6:05 - AsyncSequence might throw

      do {
          for try await quake in quakeDownload {
              ...
          }
      } catch {
          ...
      }
    • 7:15 - Concurrently iterating inside an async task

      let iteration1 = Task {
          for await quake in quakes {
              ...
          }
      }
      
      let iteration2 = Task {
          do {
              for try await quake in quakeDownload {
                  ...
              }
          } catch {
              ...
          }
      }
      
      //... later on  
      iteration1.cancel()
      iteration2.cancel()
    • 7:56 - Reading bytes from a FileHandle

      for try await line in FileHandle.standardInput.bytes.lines {
          ...
      }
    • 8:16 - Reading lines from a URL

      let url = URL(fileURLWithPath: "/tmp/somefile.txt")
      for try await line in url.lines {
          ...
      }
    • 8:49 - Reading bytes from a URLSession

      let (bytes, response) = try await URLSession.shared.bytes(from: url)
      
      guard let httpResponse = response as? HTTPURLResponse,
            httpResponse.statusCode == 200 /* OK */
      else {
          throw MyNetworkingError.invalidServerResponse
      }
      
      for try await byte in bytes {
          ...
      }
    • 9:12 - Notifications

      let center = NotificationCenter.default
      let notification = await center.notifications(named: .NSPersistentStoreRemoteChange).first {
          $0.userInfo[NSStoreUUIDKey] == storeUUID
      }
    • 11:10 - Using an AsyncStream

      class QuakeMonitor {
          var quakeHandler: (Quake) -> Void
          func startMonitoring()
          func stopMonitoring()
      }
      
      let quakes = AsyncStream(Quake.self) { continuation in
          let monitor = QuakeMonitor()
          monitor.quakeHandler = { quake in
              continuation.yield(quake)
          }
          continuation.onTermination = { @Sendable _ in
              monitor.stopMonitoring()
          }
          monitor.startMonitoring()
      }
      
      let significantQuakes = quakes.filter { quake in
          quake.magnitude > 3
      }
      
      for await quake in significantQuakes {
          ...
      }

Developer Footer

  • Vidéos
  • WWDC21
  • Meet AsyncSequence
  • 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