in IOS how `Dispatcher.Main` is working ? is it sa...
# multiplatform
p
in IOS how
Dispatcher.Main
is working ? is it safe or only
Dispatcher.Default
should be used
j
It depends what you're doing. If you need to be on the main thread for UI updates,
Dispatchers.Main
is what you want. For background work off the main thread to prevent blocking the UI, then
Dispatchers.Default
. For blocking I/O, you'd want to use
<http://Dispatchers.IO|Dispatchers.IO>
. This is similar to Android and other platforms.
πŸ™Œ 2
p
but KMM does not support main thread for IOS
j
I don't know what you're basing that information on. It certainly does.
πŸ™Œ 1
a
It is absolutely safe to use Dispatchers.Main. The were the solution to create own Dispatchers base on iOS MainDispatcher. But Dispatchers.IO or Dispatchers.Default were not supported by KMP until the release of improved memory management model on Autumn 2022. So nowadays you can use all provided Dispatchers. Just connect them via expect/actual mechanism
πŸ™Œ 1
j
Dispatchers.Main
,
Dispatchers.Default
, and
Dispatchers.Unconfined
are in the common API, available on all platforms. The platform just needs to provide a main thread dispatcher implementation, which iOS does.
<http://Dispatchers.IO|Dispatchers.IO>
is available on concurrent platforms, basically non-JS platforms, including iOS.
πŸ™Œ 1
p
@azharkova cannot we just we Dispatchers.Main in commonMain without expect/actual
a
@pers you should create both expect and actual. Common main expect : expect val uiDispatchers: Dispatchers ios main actual: actual val uiDispatchers = Dispachers.Main Even the dispathers for both platforms have same names, but ther inner references to the API are rather different
πŸ™Œ 1
πŸ†™ 1
j
You shouldn't need to expect/actual. Yes the implementations are different for each platform, but the coroutines library already handles abstracting that to a common API.
πŸ™Œ 2
p
Anna Zharkova is google expert but which one is ture
j
I use
Dispatchers.Main
in common code without issue. Some platforms, like desktop JVM, just need to provide an implementation depending on the UI toolkit used, with something like the coroutines-swing dependency.
Dispatchers.Main
works on Android and iOS out of the box.
πŸ™Œ 1
p
misz @Pamela Hill plz could confirm if Dispatchers.Main in commonMain will be working without expect/actual
p
I will ask the team to double check, but I believe it should work out of the box for Android and iOS without expect/actual as it's "common" Kotlin
πŸ™Œ 2
a
Hi @Pamela Hill is this case of compose desktop app as for in KMM project?
369 Views