Swift & SwiftUI
  • İçerikler
  • UI Bileşenleri
    • Text
    • Button
    • Image
    • Picker
    • SecureField
    • Stepper
    • Slider
    • TabView
    • Sheet
    • Action Sheet
    • Alert
  • Layout
    • Fill-Space-Equally
  • State & Data Flow
    • Content
    • EnvironmentObject
    • ObservableObject
    • ObservedObject
    • Binding
  • Gestures
    • TapGesture
    • DragGesture
    • MagnificationGesture
    • RotationGesture
    • LongPressGesture
    • Notes
  • Extra
    • GeometryReader
    • Timer
    • AlignmentGuide
    • PreferenceKey
  • Concurrency
    • Perform asynchronous operation
Powered by GitBook
On this page

Was this helpful?

  1. Concurrency

Perform asynchronous operation

Asenkron çağrımlarımızı deneyebilmek için yardımcı bir fonksiyon oluşturalım;

var dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS "
    return formatter
}()

func fetchData(_ seconds: Int) async -> Int {
    try! await Task.sleep(for: .seconds(seconds))
    let timestamp = await dateFormatter.string(from: Date())
    debugPrint("\(timestamp): Task completed in \(seconds) seconds")
    
    return seconds
}

Calling Asynchronous Functions in Series

await fetchData(1)
await fetchData(2)
await fetchData(3)

// Output;
// "2024-01-24 15:18:23.211 : Task completed in 1 seconds"
// "2024-01-24 15:18:25.247 : Task completed in 2 seconds"
// "2024-01-24 15:18:28.320 : Task completed in 3 seconds"

Async Functions in Parallel

async let first = fetchData(1)
async let second = fetchData(1)
async let third = fetchData(1)

let seconds = await [first, second, third]

// Output;
// "2024-01-24 15:19:40.256 : Task completed in 1 seconds"
// "2024-01-24 15:19:40.256 : Task completed in 1 seconds"
// "2024-01-24 15:19:40.256 : Task completed in 1 seconds"
PreviousPreferenceKey

Last updated 1 year ago

Was this helpful?