在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Autodesk/coroutineworker开源软件地址(OpenSource Url):https://github.com/Autodesk/coroutineworker开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):CoroutineWorkerSpecs
GradleTo use in your multiplatform project, update your common dependencies in your gradle configuration: kotlin {
sourceSets {
commonMain {
dependencies {
implementation "com.autodesk:coroutineworker:0.8.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') AboutCoroutineWorker 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 DevicesSample UsageSpawning Asynchronous WorkUse 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 CompleteFrom a coroutine context (i.e. somewhere you can call a 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 Waiting on Asynchronous Callback-based WorkUse 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 ProjectIn 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 CoroutineWorker Prefers Frozen StateObject 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:
Tips for Working with Frozen State
IO-Bound WorkIn the JVM world, you typically write code like this for managing IO-bound work with coroutines:
Similar behavior is supported in CoroutineWorker for Kotlin/Native via the |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论