Loading...
Loading...
Kuikly Coroutine and Multithreading Programming Assistant. Guides how to perform asynchronous programming in Kuikly, including Kuikly built-in coroutines, kotlinx coroutines, and kuiklyx coroutine library. It is used when users need to execute asynchronous tasks, switch threads, use coroutines, return to Kuikly thread to update UI, or troubleshoot thread safety issues in Kuikly.
npx skill4agent add tencent-tds/kuiklyui-ai kuikly-coroutines-threading| Feature | Callback (No Coroutine) | Kuikly Built-in Coroutine | kotlinx Coroutine |
|---|---|---|---|
| Dynamic support | ✅ Supported | ✅ Supported | ❌ Not supported |
| Dependency package size increase | None | None | kotlinx coroutine library |
| Thread safety | Not involved | Automatically guaranteed | Need to consider |
GlobalScope.launch { ... }lifecycleScope.launch { ... }async { ... }await()import com.tencent.kuikly.core.coroutines.GlobalScope
import com.tencent.kuikly.core.coroutines.launch
import com.tencent.kuikly.core.coroutines.async
import com.tencent.kuikly.core.coroutines.delay// iOS & Android
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLINX_COROUTINES_VERSION")
// HarmonyOS
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLINX_COROUTINES_OHOS_VERSION")Dispatchers.KuiklyKuiklyContextScheduler// iOS & Android
implementation("com.tencent.kuiklyx:coroutines:$KUIKLYX_COROUTINES_VERSION")
// HarmonyOS
implementation("com.tencent.kuiklyx:coroutines:$KUIKLYX_COROUTINES_OHOS_VERSION")// Launch coroutine, execute in Kuikly thread
GlobalScope.launch(Dispatchers.Kuikly[ctx]) { ... }
// Switch to Kuikly thread in coroutine
withContext(Dispatchers.Kuikly[ctx]) { ... }KuiklyContextScheduler.runOnKuiklyThread(pagerId) { cancel ->
if (cancel) return // Pager has been destroyed
// Execute in Kuikly thread
}Need asynchronous programming?
├── Only need to simplify callbacks, no multi-threading requirements?
│ └── → Option 1: Kuikly Built-in Coroutines
├── Need to execute time-consuming tasks + support dynamic scenarios?
│ └── → Option 2: Module Mechanism (can be used with built-in coroutines)
├── Need multi-threading + no dynamic scenario support required?
│ └── → Option 3: kotlinx coroutines + kuiklyx coroutines
└── Compose DSL page?
└── → Option 4: LaunchedEffect + withContext⚠️ Consistency Principle: When selecting a solution, you should also pay attention to the existing asynchronous programming methods in the current project or module, and prioritize consistency to avoid mixing multiple sets of asynchronous solutions in the same module.
ComposeContainer@Composable
fun MyContent() {
var data by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
// Default execution in Kuikly thread (via ComposeDispatcher)
val result = withContext(Dispatchers.IO) {
// Execute time-consuming task in IO thread
fetchData()
}
// Automatically return to Kuikly thread
data = result
}
Text(text = data)
}LaunchedEffectComposeDispatcherwithContext(Dispatchers.IO)withContextviewModelScope// Declare expect in commonMain
internal expect val Dispatchers.IO: CoroutineDispatcher
// androidMain / appleMain / ohosArm64Main
internal actual val Dispatchers.IO: CoroutineDispatcher
get() = Dispatchers.IO // Use platform-native IO dispatcher
// jsMain (does not support multi-threading)
internal actual val Dispatchers.IO: CoroutineDispatcher
get() = Dispatchers.Defaultoverride fun willInit() {
super.willInit()
Pager.VERIFY_THREAD = true // Enable thread verification
Pager.VERIFY_REACTIVE_OBSERVER = true // Enable observable verification
Pager.verifyFailed { exception -> // Custom verification failure handling
println("Thread safety verification failed: ${exception.message}")
throw exception
}
}| ❌ Wrong Practice | ✅ Correct Practice |
|---|---|
| Directly update observable in non-Kuikly thread | Switch back to Kuikly thread via |
| Call Module methods in non-Kuikly thread | Module methods like acquireModule / toNative must be called in Kuikly thread |
| Use kotlinx coroutines in dynamic scenarios | Use Kuikly built-in coroutines or Module mechanism |
| Call built-in coroutine APIs outside Kuikly thread | Built-in coroutine APIs can only be called in Kuikly thread |
| Forget to handle callbacks after Pager destruction | Check |
Operate UI in | Verification failures usually occur in non-Kuikly threads, cannot call UI APIs |
| Update State in IO thread in Compose | Update State after returning to default dispatcher via |