Skip to content

Use default dispatcher for phoenix-shared coroutine scopes#780

Merged
dpad85 merged 1 commit intomasterfrom
use-default-dispatchers
Nov 12, 2025
Merged

Use default dispatcher for phoenix-shared coroutine scopes#780
dpad85 merged 1 commit intomasterfrom
use-default-dispatchers

Conversation

@dpad85
Copy link
Member

@dpad85 dpad85 commented Nov 7, 2025

We've been used the MainScope until now for all our coroutines, a scope that relies on the UI dispatcher. This is not suited for many of the phoenix-shared coroutines which are doing background stuff and should not run on the UI thread. I believe it was done this way for historical reasons, back when kotlin multiplatform had issues with background operations on iOS, but that was fixed when we adopted the new memory model (see #299).

This PR defines a defaultScope method that uses the predefined Default dispatcher:

uses a common pool of shared background threads (...) an appropriate choice for compute-intensive coroutines that consume CPU resources.
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/

There's also a Dispatcher.IO dispatcher that could be used for coroutines doing I/O operations.

Note: we've been already using Dispatchers.Default for database queries, but we did that by explicitly switching context with withContext. @robbiehanson I think that this PR could allow us to remove those switches and simplify code (and also ensure we're not doing db stuff on the Main thread).

This replaces MainScope which uses the UI dispatcher.
@dpad85 dpad85 marked this pull request as ready for review November 10, 2025 10:25
@robbiehanson robbiehanson self-requested a review November 10, 2025 15:48
Copy link
Contributor

@robbiehanson robbiehanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. And the iOS app feels more responsive now.

@dpad85 dpad85 merged commit a666373 into master Nov 12, 2025
@dpad85 dpad85 deleted the use-default-dispatchers branch November 12, 2025 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants