From ada8766c8d3805498248ae99fbb5e8dbbc43380b Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Tue, 11 Jun 2024 12:56:44 +0200 Subject: [PATCH] coroutine_read_actions.md: Add information about the coroutine equivalent of using finishOnUiThread to start a write action --- .../coroutines/coroutine_read_actions.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/topics/basics/architectural_overview/coroutines/coroutine_read_actions.md b/topics/basics/architectural_overview/coroutines/coroutine_read_actions.md index aafb9db68..dd1c95c67 100644 --- a/topics/basics/architectural_overview/coroutines/coroutine_read_actions.md +++ b/topics/basics/architectural_overview/coroutines/coroutine_read_actions.md @@ -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: