```class AppSession internal constructor(globalSco...
# kotlin-native
m
Copy code
class AppSession internal constructor(globalScope: GlobalScope) {

    private val appRepositoryFactory = globalScope.appRepositoryFactory

    init {
        CoroutineScope(Dispatchers.Default).launch {
            withContext(Dispatchers.Default) {
                appRepositoryFactory.profilesRepository
            }
        }
    }
this is not
Copy code
class AppSession internal constructor(globalScope: GlobalScope) {

    init {
        CoroutineScope(Dispatchers.Default).launch {
            withContext(Dispatchers.Default) {
                globalScope.appRepositoryFactory.profilesRepository
            }
        }
    }
did someone had something weird? nothing is happening inside this “profilesRepository”. Without coroutines it also works. Error is:
Copy code
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen pl.app.di.AppSession@848e28
    at 0   MobileBankShared                    0x0000000110718fcd kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
btw
Copy code
class AppSession internal constructor(globalScope: GlobalScope) {


    init {
        val appRepositoryFactory = globalScope.appRepositoryFactory
        CoroutineScope(Dispatchers.Default).launch {
            withContext(Dispatchers.Default) {
                appRepositoryFactory.pinRepository
            }
        }
    }
this also works, looks like a bug to me
a
I guess its all because coroutines can cross threads and everything captured in the launch is going to be frozen. So in your example, when you are passing newly created variable
appRepositoryFactory
, it frozes and everything is ok. In the second example, you are passing down
globalScope
and I guess it can't be frozen because it can contain state.
m
Had similar thoughts, still looks wrong to me