kmp

KMPNativeCoroutinesCombine

Library to use Kotlin Coroutines from Swift code in KMP apps

Showing:

Popularity

Downloads/wk

0

GitHub Stars

125

Maintenance

Last Commit

19d ago

Contributors

1

Package

Dependencies

1

License

MIT

Categories

Readme

KMP-NativeCoroutines

A library to use Kotlin Coroutines from Swift code in KMP apps.

Why this library?

Both KMP and Kotlin Coroutines are amazing but together they have some limitations.

The most important limitation is cancellation support.
Kotlin suspend functions are exposed to Swift as functions with a completion handler.
This allows you to easily use them from your Swift code, but it doesn't support cancellation.

While Swift 5.5 brings async functions to Swift, it doesn't solve this issue.
For interoperability with ObjC all function with a completion handler can be called like an async function.
This means starting with Swift 5.5 your Kotlin suspend functions will look like Swift async functions.
But that's just syntactic sugar, so there's still no cancellation support.

Besides cancellation support, ObjC doesn't support generics on protocols.
So all the Flow interfaces lose their generic value type which make them hard to use.

This library solves both of these limitations 😄 .

Compatibility

As of version 0.6.0 the library uses Kotlin version 1.5.30.
Compatibility versions for older Kotlin versions are also available:

VersionVersion suffixKotlinCoroutines
latestno suffix1.5.301.5.2-native-mt
latest-kotlin-1.5.201.5.201.5.0-native-mt
0.4.3-kotlin-1.5.101.5.101.5.0-native-mt

You can choose from a couple of Swift implementations.
Depending on the implementation you can support as low as iOS 9, macOS 10.9, tvOS 9 and watchOS 3:

ImplementationSwiftiOSmacOStvOSwatchOS
RxSwift5.09.010.99.03.0
Combine5.013.010.1513.06.0
Async 🚧5.515.012.015.08.0

🚧 : the Async implementation requires Xcode 13 which is currently in beta!

Installation

The library consists of a Kotlin and Swift part which you'll need to add to your project.
The Kotlin part is available on Maven Central and the Swift part can be installed via CocoaPods.

Make sure to always use the same versions for all the libraries!

latest release

Kotlin

For Kotlin just add the plugin to your build.gradle.kts:

plugins {
    id("com.rickclephas.kmp.nativecoroutines") version "<version>"
}

Swift

Now for Swift you can choose from a couple of implementations.
Add one or more of the following libraries to your Podfile:

pod 'KMPNativeCoroutinesCombine'  # Combine implementation
pod 'KMPNativeCoroutinesRxSwift'  # RxSwift implementation
pod 'KMPNativeCoroutinesAsync'    # Swift 5.5 Async/Await implementation

Usage

Using your Kotlin Coroutines code from Swift is almost as easy as calling the Kotlin code.
Just use the wrapper functions in Swift to get Observables, Publishers, AsyncStreams or async functions.

Kotlin

The plugin will automagically generate the necessary code for you! 🔮

Your Flow properties/functions get a Native version:

class Clock {
    // Somewhere in your Kotlin code you define a Flow property
    val time: StateFlow<Long> // This can be any kind of Flow

    // The plugin will generate this native property for you
    val timeNative
        get() = time.asNativeFlow()
}

In case of a StateFlow or SharedFlow property you also get a NativeValue or NativeReplayCache property:

// For the StateFlow defined above the plugin will generate this native value property
val timeNativeValue
    get() = time.value

// In case of a SharedFlow the plugin would generate this native replay cache property
val timeNativeReplayCache
    get() = time.replayCache

The plugin also generates Native versions for all your suspend functions:


class RandomLettersGenerator {
    // Somewhere in your Kotlin code you define a suspend function
    suspend fun getRandomLetters(): String { 
        // Code to generate some random letters
    }

    // The plugin will generate this native function for you
    fun getRandomLettersNative() = 
        nativeSuspend { getRandomLetters() }
}

Global properties and functions

The plugin is currently unable to generate native versions for global properties and functions.
In such cases you have to manually create the native versions in your Kotlin native code.

Custom suffix

If you don't like the naming of these generated properties/functions, you can easily change the suffix.
For example add the following to your build.gradle.kts to use the suffix Apple:

nativeCoroutines {
    suffix = "Apple"
}

Custom CoroutineScope

For more control you can provide a custom CoroutineScope with the NativeCoroutineScope annotation:

class Clock {
    @NativeCoroutineScope
    internal val coroutineScope = CoroutineScope(job + Dispatchers.Default)
}

If you don't provide a CoroutineScope the default scope will be used which is defined as:

@SharedImmutable
internal val defaultCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

Ignoring declarations

Use the NativeCoroutinesIgnore annotation to tell the plugin to ignore a property or function:

@NativeCoroutinesIgnore
val ignoredFlowProperty: Flow<Int>

@NativeCoroutinesIgnore
suspend fun ignoredSuspendFunction() { }

RxSwift

The RxSwift implementation provides a couple functions to get an Observable or Single for your Coroutines code.

For your Flows use the createObservable(for:) function:

// Create an observable for your flow
let observable = createObservable(for: clock.timeNative)

// Now use this observable as you would any other
let disposable = observable.subscribe(onNext: { value in
    print("Received value: \(value)")
}, onError: { error in
    print("Received error: \(error)")
}, onCompleted: {
    print("Observable completed")
}, onDisposed: {
    print("Observable disposed")
})

// To cancel the flow (collection) just dispose the subscription
disposable.dispose()

For the suspend functions you should use the createSingle(for:) function:

// Create a single for the suspend function
let single = createSingle(for: randomLettersGenerator.getRandomLettersNative())

// Now use this single as you would any other
let disposable = single.subscribe(onSuccess: { value in
    print("Received value: \(value)")
}, onFailure: { error in
    print("Received error: \(error)")
}, onDisposed: {
    print("Single disposed")
})

// To cancel the suspend function just dispose the subscription
disposable.dispose()

You can also use the createObservable(for:) function for suspend functions that return a Flow:

let observable = createObservable(for: randomLettersGenerator.getRandomLettersFlowNative())

Note: these functions create deferred Observables and Singles.
Meaning every subscription will trigger the collection of the Flow or execution of the suspend function.

Combine

The Combine implementation provides a couple functions to get an AnyPublisher for your Coroutines code.

For your Flows use the createPublisher(for:) function:

// Create an AnyPublisher for your flow
let publisher = createPublisher(for: clock.timeNative)

// Now use this publisher as you would any other
let cancellable = publisher.sink { completion in
    print("Received completion: \(completion)")
} receiveValue: { value in
    print("Received value: \(value)")
}

// To cancel the flow (collection) just cancel the publisher
cancellable.cancel()

For the suspend functions you should use the createFuture(for:) function:

// Create a Future/AnyPublisher for the suspend function
let future = createFuture(for: randomLettersGenerator.getRandomLettersNative())

// Now use this future as you would any other
let cancellable = future.sink { completion in
    print("Received completion: \(completion)")
} receiveValue: { value in
    print("Received value: \(value)")
}

// To cancel the suspend function just cancel the future
cancellable.cancel()

You can also use the createPublisher(for:) function for suspend functions that return a Flow:

let publisher = createPublisher(for: randomLettersGenerator.getRandomLettersFlowNative())

Note: these functions create deferred AnyPublishers.
Meaning every subscription will trigger the collection of the Flow or execution of the suspend function.

Swift 5.5 Async/Await

🚧 : the Async implementation requires Xcode 13 which is currently in beta!

The Async implementation provides some functions to get async Swift functions and AsyncStreams.

Use the asyncFunction(for:) function to get an async function that can be awaited:

let handle = Task {
    do {
        let letters = try await asyncFunction(for: randomLettersGenerator.getRandomLettersNative())
        print("Got random letters: \(letters)")
    } catch {
        print("Failed with error: \(error)")
    }
}

// To cancel the suspend function just cancel the async task
handle.cancel()

or if you don't like these do-catches you can use the asyncResult(for:) function:

let result = await asyncResult(for: randomLettersGenerator.getRandomLettersNative())
if case let .success(letters) = result {
    print("Got random letters: \(letters)")
}

For Flows there is the asyncStream(for:) function to get an AsyncStream:

let handle = Task {
    do {
        let stream = asyncStream(for: randomLettersGenerator.getRandomLettersFlowNative())
        for try await letters in stream {
            print("Got random letters: \(letters)")
        }
    } catch {
        print("Failed with error: \(error)")
    }
}

// To cancel the flow (collection) just cancel the async task
handle.cancel()

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100