# Working with Kotlin Coroutines and RxSwift

DevFeed: [Working with Kotlin Coroutines and RxSwift](<https://devfeed.tech/articles/working-with-kotlin-coroutines-and-rxswift-25005.md>)

Original publisher: [Read original article](<https://dev.to/touchlab/working-with-kotlin-coroutines-and-rxswift-24fa>)

Author: Russell Wolf

Published: 2020-06-15T19:07:18Z

Content type: tutorial

Language: en

Sources: [DEV Community 👩💻👨💻: Russell Wolf](<https://devfeed.tech/sources/dev-community-russell-wolf.md>)

Topics: [kotlin-coroutines](<https://devfeed.tech/topics/kotlin-coroutines.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [reactive](<https://devfeed.tech/topics/reactive.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [coding](<https://devfeed.tech/tags/coding.md>), [community](<https://devfeed.tech/tags/community.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [development](<https://devfeed.tech/tags/development.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [flow](<https://devfeed.tech/tags/flow.md>), [inclusive](<https://devfeed.tech/tags/inclusive.md>), [interop](<https://devfeed.tech/tags/interop.md>), [ios](<https://devfeed.tech/tags/ios.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [kotlinmultiplatform](<https://devfeed.tech/tags/kotlinmultiplatform.md>), [memory-leaks](<https://devfeed.tech/tags/memory-leaks.md>), [reactive](<https://devfeed.tech/tags/reactive.md>), [reactive-streams](<https://devfeed.tech/tags/reactive-streams.md>), [software](<https://devfeed.tech/tags/software.md>), [suspend](<https://devfeed.tech/tags/suspend.md>), [swift](<https://devfeed.tech/tags/swift.md>)

## AI overview

This tutorial explains interoperability between Kotlin coroutines in shared code and RxSwift on iOS. It describes coroutine limitations on Kotlin/Native, including single-threaded release use and experimental multithreading risks, and discusses Swift and Objective-C interop constraints. Example repository code demonstrates single-event and stream operations, success and error cases, and cancellation.

## Source excerpt

A recent client engagement involved interop between Kotlin coroutines in shared code, and RxSwift on the iOS side. We did some work to ensure that this could be done in a way that is type-safe and thread-safe. Whether or not you use RxSwift, hopefully this can provide some useful patterns for interop code. Coroutines are a Kotlin language feature that allows asynchronous code to be written in a way that looks like synchronous code, avoiding the nesting that often comes with callback-based APIs. They're available on all Kotlin platforms, but have some limitations on the native side. The release version of native coroutines is limited to single-threaded use-cases, though there are experimental releases available that are multithreaded with some risk of memory leaks. But while they're a fully-supported language feature of Kotlin, they don't translate to Objective-C and Swift. RxSwift is a Swift implementation of the Reactive Streams specification. It's one way to handle asynchronous code on Swift, and has many operators for combining and transforming event streams. Though some of the names are different, much of the API will feel familiar to Kotlin developers who have experience with RxJava. Since Coroutines will almost always be present in shared code, and RxSwift is a common option on the iOS side, hopefully the motivation to communicate between them is clear. So let's start writing some code. Common Repository We'll work with a dummy repository class that looks like this, defined in src/commonMain class ThingRepository { suspend fun getThing(succeed: Boolean): Thing { delay(100) if (succeed) { return Thing(0) } else { error("oh no!") } } fun getThingStream(count: Int, succeed: Boolean): Flow<Thing> = flow { repeat(count) { delay(100) emit(Thing(it)) } if (!succeed) error("oops!") } } From the outside this looks roughly like a real repository might, but with inputs that let us control the output a bit more directly for demonstration purposes. It gives us the ability