Skip to content

Commit e31c2f8

Browse files
Use Main dispatcher for shareIn
Otherwise the sharing fails with the error: "Cannot start an undispatched coroutine in another thread DefaultDispatcher from current MainThread" This is supposedly fixed Kotlin/kotlinx.coroutines#3136, but for some reason, the fix doesn't work when the new memory model is enabled
1 parent 6213ef9 commit e31c2f8

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

shared/src/androidMain/kotlin/com/hicham/wcstoreapp/data/currencyformat/StoreCurrencyFormatProvider.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import com.hicham.wcstoreapp.data.api.WooCommerceApi
99
import com.hicham.wcstoreapp.data.api.toDomainModel
1010
import com.hicham.wcstoreapp.models.CurrencyFormatSettings
1111
import kotlinx.coroutines.CoroutineScope
12+
import kotlinx.coroutines.Dispatchers
1213
import kotlinx.coroutines.flow.*
1314
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.plus
1416
import kotlinx.serialization.decodeFromString
1517
import kotlinx.serialization.encodeToString
1618
import kotlinx.serialization.json.Json
@@ -40,7 +42,7 @@ class StoreCurrencyFormatProvider(
4042
// Refresh the format settings when the flow is restarted
4143
fetchFormatSettings()
4244
}
43-
.shareIn(coroutineScope, SharingStarted.WhileSubscribed(60000L), replay = 1)
45+
.shareIn(coroutineScope + Dispatchers.Main, SharingStarted.WhileSubscribed(60000L), replay = 1)
4446

4547
private fun fetchFormatSettings() {
4648
coroutineScope.launch {

shared/src/commonMain/kotlin/com/hicham/wcstoreapp/data/address/db/DBAddressRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.hicham.wcstoreapp.util.runCatchingNetworkErrors
1515
import kotlinx.coroutines.CoroutineScope
1616
import kotlinx.coroutines.Dispatchers
1717
import kotlinx.coroutines.flow.*
18+
import kotlinx.coroutines.plus
1819
import kotlinx.coroutines.withContext
1920

2021
class DBAddressRepository(
@@ -26,7 +27,7 @@ class DBAddressRepository(
2627
) : AddressRepository {
2728
private val _savedAddresses = addressDao.getSavedAddresses()
2829
.shareIn(
29-
scope = appCoroutineScope,
30+
scope = appCoroutineScope + Dispatchers.Main,
3031
started = SharingStarted.WhileSubscribed(replayExpirationMillis = 0),
3132
replay = 1
3233
)

shared/src/commonMain/kotlin/com/hicham/wcstoreapp/data/cart/db/DBCartRepository.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import com.hicham.wcstoreapp.models.Product
1212
import com.hicham.wcstoreapp.util.runCatchingNetworkErrors
1313
import com.ionspin.kotlin.bignum.decimal.BigDecimal
1414
import kotlinx.coroutines.CoroutineScope
15+
import kotlinx.coroutines.Dispatchers
1516
import kotlinx.coroutines.flow.*
1617
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.plus
1719

1820
class DBCartRepository constructor(
1921
private val cartDao: CartDao,
@@ -34,7 +36,7 @@ class DBCartRepository constructor(
3436
fetchCart()
3537
}
3638
.distinctUntilChanged()
37-
.shareIn(appCoroutineScope, started = SharingStarted.WhileSubscribed(60000), replay = 1)
39+
.shareIn(appCoroutineScope + Dispatchers.Main, started = SharingStarted.WhileSubscribed(60000), replay = 1)
3840

3941
private fun fetchCart() = appCoroutineScope.launch {
4042
runCatchingNetworkErrors {

0 commit comments

Comments
 (0)