All Projects → Autodesk → Coroutineworker

Autodesk / Coroutineworker

Licence: apache-2.0
Kotlin Coroutine-based workers for native

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Coroutineworker

AndroidCoroutineScopes
This lib implements the most common CoroutineScopes used in Android apps.
Stars: ✭ 14 (-95.04%)
Mutual labels:  coroutines, kotlin-coroutines
WanAndroidJetpack
🔥 WanAndroid 客户端,Kotlin + MVVM + Jetpack + Retrofit + Glide。基于 MVVM 架构,用 Jetpack 实现,网络采用 Kotlin 的协程和 Retrofit 配合使用!精美的 UI,便捷突出的功能实现,欢迎下载体验!
Stars: ✭ 124 (-56.03%)
Mutual labels:  coroutines, kotlin-coroutines
NewsReader
Android News Reader app. Kotlin Coroutines, Retrofit and Realm
Stars: ✭ 21 (-92.55%)
Mutual labels:  coroutines, kotlin-coroutines
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (-87.59%)
Mutual labels:  coroutines, kotlin-coroutines
Keemun
No description or website provided.
Stars: ✭ 13 (-95.39%)
Mutual labels:  coroutines, multiplatform
kotlin-coroutines-android
Useful extensions for coroutines. AutoDispose + MainScope
Stars: ✭ 84 (-70.21%)
Mutual labels:  coroutines, kotlin-coroutines
android-clean-arc-coroutines
Clean Architecture(Coroutines,Dagger, MVVM, ROOM, retrofit, databinding)
Stars: ✭ 116 (-58.87%)
Mutual labels:  coroutines, kotlin-coroutines
the-white-rabbit
The White Rabbit is an asynchronous RabbitMQ (AMQP) client based on Kotlin coroutines
Stars: ✭ 90 (-68.09%)
Mutual labels:  coroutines, kotlin-coroutines
Retrofit2-Flow-Call-Adapter
A Retrofit 2 adapter for Kotlin Flows.
Stars: ✭ 41 (-85.46%)
Mutual labels:  coroutines, kotlin-coroutines
korte
Kotlin cORoutines Template Engine for Multiplatform Kotlin
Stars: ✭ 69 (-75.53%)
Mutual labels:  coroutines, kotlin-coroutines
StarWarsSearch-MVI
Star wars sample android project showcasing the use of View components for rendering UI in Fragments and Activities. Uses Android Jetpack, clean architecture with MVI (Uni-directional data flow), dagger hilt, and kotlin coroutines with StateFlow
Stars: ✭ 189 (-32.98%)
Mutual labels:  coroutines, kotlin-coroutines
fluid-mongo
Kotlin coroutine support for MongoDB built on top of the official Reactive Streams Java Driver
Stars: ✭ 27 (-90.43%)
Mutual labels:  coroutines, kotlin-coroutines
CoroutineLite
Simple implementation of kotlinx.coroutines to clarify the design of Kotlin Coroutines.
Stars: ✭ 142 (-49.65%)
Mutual labels:  coroutines, kotlin-coroutines
mqtt
Kotlin cross-platform, coroutine based, reflectionless MQTT 3.1.1 & 5.0 client & server
Stars: ✭ 31 (-89.01%)
Mutual labels:  coroutines, kotlin-coroutines
Delish
Delish, a Food Recipes App in Jetpack Compose and Hilt based on modern Android tech-stacks and MVI clean architecture.
Stars: ✭ 356 (+26.24%)
Mutual labels:  coroutines, kotlin-coroutines
SketchwareManager
Coroutine-based library for managing Sketchware (Sketchware Pro/Studio) projects, collections and etc.
Stars: ✭ 54 (-80.85%)
Mutual labels:  coroutines, kotlin-coroutines
Kaskade
[INACTIVE] Simplifying state management
Stars: ✭ 223 (-20.92%)
Mutual labels:  coroutines, multiplatform
Oolong
MVU for Kotlin Multiplatform
Stars: ✭ 248 (-12.06%)
Mutual labels:  coroutines, multiplatform
BlueFlow
Android Bluetooth classic API wrapped in Coroutines Flow.
Stars: ✭ 64 (-77.3%)
Mutual labels:  coroutines, kotlin-coroutines
jda-ktx
Collection of useful Kotlin extensions for JDA
Stars: ✭ 49 (-82.62%)
Mutual labels:  coroutines, kotlin-coroutines

CoroutineWorker

Build Status

Maven Central

Specs

  • Supported on Native, JVM, and JS (legacy and IR) (feel free to contribute adding more targets)
  • Kotlin 1.4.0

Gradle

To use in your multiplatform project, update your common dependencies in your gradle configuration:

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation "com.autodesk:coroutineworker:0.6.2"
            }
        }
    }
}

CoroutineWorker uses gradle module metadata. We recommend adding the following to your settings.gradle to take advantage of that (not necessary for Gradle 6+):

enableFeaturePreview('GRADLE_METADATA')

About

CoroutineWorker helps support multi-threaded coroutine usage in common code that works in Kotlin/Native and on JVM until kotlinx.coroutines has full support for native, multi-threaded coroutines.

Projects Using this on your Devices

Sample Usage

Spawning Asynchronous Work

Use execute to start background work from common code:

val worker = CoroutineWorker.execute {
  // - In here, `this` is a `CoroutineScope`
  // - Run suspend functions, call launch, etc.
  // - This code runs in a thread pool
}

// Tells the worker to cancel (uses standard coroutine cancellation)
worker.cancel()

// Tells the worker to cancel; it suspends until cancellation is finished
worker.cancelAndJoin()

Waiting on Asynchronous Work to Complete

From a coroutine context (i.e. somewhere you can call a suspend fun), use withContext to kick off work to another thread. It will non-blocking/suspend wait for the cross-thread work to complete:

suspend fun doWork() {
  val result = CoroutineWorker.withContext {
    // This is similar to execute, but it returns
    // the result of the work at the end of this lambda
    1
  }
  print(result) // prints 1
}

This is like using withContext on JVM to switch coroutine contexts. You can also properly pass a dispatcher, which will be used on JVM: withContext(Dispatchers.IO) { … }. The idea here is that this will be easy to migrate when we do get multi-threaded coroutine support in Kotlin/Native.

Waiting on Asynchronous Callback-based Work

Use threadSafeSuspendCallback to bridge callback-style async work into your code as a suspend fun:

suspend fun performNetworkFetch() {
  val result = threadSafeSuspendCallback { completion ->
    // example: fetch network data that isn't coroutine-compatible
    fetchNetworkData { networkResult ->
      // notify that async work is complete
      completion(networkResult)
    }
  }

  // result is now available here
}

Sample Project

In the sample directory, there is a sample project that demonstrates adding CoroutineWorker to an iOS + JVM library. We just used the sample library from IntelliJ's template for a "Mobile Shared Library." In the sample is a function called performWork (common code) that takes a completion lambda and demonstrates CoroutineWorker.execute. In tests, we use K/N concurrency helpers from kotlinx.atomicfu to demonstrate capturing a result across threads in K/N and executing this function.

CoroutineWorker Prefers Frozen State

Object detachment (i.e. transferring object ownership from one thread to another) is relatively difficult to achieve (outside of simple scenarios) compared to working with objects that are frozen and immutable. Because of this, CoroutineWorker prefers taking the frozen, immutable route:

  • Lambdas passed to CoroutineWorker are automatically frozen when they are going to be passed across threads.
  • The result value from withContext is also frozen.

Tips for Working with Frozen State

  • Be careful about what your frozen lambdas capture; those objects will be frozen too. Especially, watch for implicit references to this.
  • Call ensureNeverFrozen() on objects that you don't expect to ever be frozen.

IO-Bound Work

In the JVM world, you typically write code like this for managing IO-bound work with coroutines:

withContext(Dispatchers.IO) {
    // IO writes
}

Similar behavior is supported in CoroutineWorker for Kotlin/Native via the IODispatcher. To use it in common code, make an expect val Dispatchers.IO: CoroutineDispatcher that returns IODispatcher for Kotlin/Native and Dispatchers.IO for JVM, and pass that to CoroutineWorker.withContext when performing IO-bound worker.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].