Sitemap
MadLeo

MadLeo

Passionate mobile engineer focused on building intuitive apps and sharing insights on Swift, Kotlin, and modern development practices through technical writing.

Getting Started with Kotlin Coroutines and SwiftUI Async/Await: Managing Asynchronous Tasks

3 min readAug 18, 2025

--

Modern mobile apps rely heavily on asynchronous tasks — fetching data from APIs, updating UI without blocking the main thread, or handling background work like saving files. Writing async code used to be messy, full of callbacks and nested closures.

Press enter or click to view image in full size
Photo by Kier in Sight Archives on Unsplash

Fortunately, both Android and iOS have evolved:

  • Kotlin Coroutines on Android
  • Swift Concurrency (async/await) on iOS

Both provide a cleaner, structured, and more readable way to manage async code. Let’s dive in.

🌐 Why Async Matters in Mobile Development

  • Smooth UX → No blocking the UI thread while fetching data.
  • Better Performance → Tasks like network requests or image processing happen in the background.
  • Code Readability → Async APIs are easier to follow when using Coroutines or async/await.

🤖 Kotlin Coroutines Basics

In Kotlin, Coroutines provide a way to write async code sequentially. Instead of callbacks, you launch tasks in a coroutine scope.

Example: Fetching Data with Coroutines

import kotlinx.coroutines.*

fun main() = runBlocking {
launch {
val data = fetchData()
println("Data: $data")
}
}
suspend fun fetchData(): String {
delay(2000) // Simulate network delay
return "Hello from Kotlin Coroutines!"
}

✅ Key points:

  • suspend functions can run asynchronously.
  • delay() simulates non-blocking delay.
  • runBlocking and launch start coroutines.

In Android, you’d typically use viewModelScope.launch { ... } inside a ViewModel to keep UI safe.

🍏 Swift Concurrency with Async/Await

Swift introduced async/await in iOS 15+, making async code just as clean.

Example: Fetching Data with Async/Await

import SwiftUI

func fetchData() async -> String {
try? await Task.sleep(nanoseconds: 2_000_000_000) // 2 seconds
return "Hello from Swift async/await!"
}
Task {
let data = await fetchData()
print(data)
}

✅ Key points:

  • async functions run asynchronously.
  • await waits for results without blocking.
  • Task { } lets you run async code in SwiftUI views.

📱 Android vs iOS: Side-by-Side

Press enter or click to view image in full size

🛠️ Real-World Use Cases

  1. API Calls
  • Android: viewModelScope.launch { repository.getData() }
  • iOS: Task { let data = await api.fetchData() }
  1. Background Work
  • Image processing, file I/O, analytics uploads.
  1. UI Updates
  • Always update UI on the main thread (Android: withContext(Dispatchers.Main), iOS: SwiftUI handles it automatically).

🔮 Final Thoughts

Both Kotlin Coroutines and Swift async/await bring structured concurrency to mobile development, reducing boilerplate and making async code readable, safe, and testable.

Get MadLeo’s stories in your inbox

Join Medium for free to get updates from this writer.

If you’re an Android developer — start integrating Coroutines into your ViewModels and repositories.
If you’re an iOS developer — embrace async/await and ditch the old completion handler callbacks.

👉 The future of mobile is declarative UI + structured concurrency. Mastering both will set you apart as a modern mobile developer.

--

--

MadLeo
MadLeo

Passionate mobile engineer focused on building intuitive apps and sharing insights on Swift, Kotlin, and modern development practices through technical writing.