coroutine_read_actions.md: Add information about the coroutine equivalent of using finishOnUiThread to start a write action

This commit is contained in:
Karol Lewandowski 2024-06-11 12:56:44 +02:00
parent 14ee83823b
commit ada8766c8d

View File

@ -45,6 +45,17 @@ WARA doesn't need the following API methods:
- `expireWhen(BooleanSupplier expireCondition)`/`expireWith(Disposable parentDisposable)`/`wrapProgress(ProgressIndicator progressIndicator)` because they are canceled when the calling coroutine is canceled
- `finishOnUiThread()` because this is handled by switching to the [EDT dispatcher](coroutine_dispatchers.md#edt-dispatcher).
Note that the UI data must be pure (e.g., strings/icons/element pointers), which inherently cannot be invalidated during the transfer from a background thread to EDT.
In the case of using NBRA's `finishOnUiThread` to start a write action, the coroutine equivalent is `readAndWriteAction`:
```kotlin
readAndWriteAction {
val computedData = computeDataInReadAction()
writeAction {
applyData(computedData)
}
}
```
It provides the same guarantees as `finishOnUIThread` (no WA between `computeDataInReadAction` and `applyData`), but it is not bound to EDT.
- `coalesceBy(Object ... equality)` because this should be handled by [`Flow.collectLatest()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect-latest.html) and/or [`Flow.distinctUntilChanged()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/distinct-until-changed.html).
Usually, NBRAs are run as a reaction to user actions, and there might be multiple NBRAs running, even if their results are unused.
Instead of cancelling the read action, in the coroutine world the coroutines are canceled: