0Pricing
Kotlin Multiplatform Academy · Aula

Funções suspend como async do Swift

Consuma corrotinas Kotlin a partir da concorrência do Swift

Funções suspend como async do Swift é uma aula grátis de Kotlin Multiplatform Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Kotlin Multiplatform Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Kotlin Multiplatform Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Suspend, Meet async

A Kotlin suspend function is exposed to Swift as an async method, so iOS can await it with native Swift concurrency.

suspend fun loadUser(): User

No Callbacks Needed

Older bridges forced completion handlers. Modern KMP maps suspend straight to async, so Swift code stays clean and linear.

Await It in Swift

From Swift you simply await the call inside an async context. The coroutine runs and resumes Swift when it finishes.

let user = try await repo.loadUser()

Errors Become throws

If the suspend function can fail, Swift sees it as a throwing async method, so you wrap the await in try and catch errors.

do { try await repo.loadUser() }
catch { print(error) }

Cancellation Carries Over

When Swift cancels its Task, that cancellation propagates into the Kotlin coroutine, stopping the shared work cleanly.

Run It in a Task

Call your async shared function from inside a Swift Task when you are not already in an async context, such as a button tap.

Task {
    let u = try await repo.loadUser()
}

Pick the Right Dispatcher

The coroutine runs on whatever dispatcher you set in shared code, so heavy work stays off the iOS main thread by default.

Returning Values

Whatever the suspend function returns becomes the result of the Swift await, mapped through the usual Kotlin-to-Obj-C rules.

Unit Means Void

A suspend function returning Unit shows up in Swift as an async method that simply returns nothing, just like void.

Generated by SKIE

Plain KMP gives you basic async support, but tools like SKIE generate even smoother Swift signatures for these suspend calls.

Linear and Readable

The big win is readability: shared async logic reads top-to-bottom in Swift, with no nested callbacks or completion pyramids.

Quick Check

Let us check how a suspend function feels from Swift.

Recap

Kotlin suspend becomes Swift async: await it in a Task, catch thrown errors, and cancellation flows both ways. Clean concurrency. ✅

Perguntas Frequentes

A aula “Funções suspend como async do Swift” é grátis?

Sim — o texto completo de “Funções suspend como async do Swift” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Kotlin Multiplatform Academy, atualize para CoddyKit PRO. O curso de Kotlin Multiplatform Academy inclui 4 aulas no total.

O que vou aprender em “Funções suspend como async do Swift”?

Consuma corrotinas Kotlin a partir da concorrência do Swift Você pratica Kotlin Multiplatform Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Kotlin Multiplatform Academy?

Nenhuma experiência prévia é necessária. Kotlin Multiplatform Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Funções suspend como async do Swift”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Kotlin Multiplatform Academy?

Sim. Cada aula de Kotlin Multiplatform Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Como Kotlin é mapeado para Objective-C
  2. Funções suspend como async do Swift
  3. Colete Flows a partir do Swift
  4. @ObjCName e APIs amigáveis ao Swift
← Voltar para Kotlin Multiplatform Academy