Was trying to run the template, and it runs fine w...
# compose-ios
h
Was trying to run the template, and it runs fine with the button and text, wanted to play a bit more around flows and ui state mutation, Considering that I may want to run an API call or anything which may be needed to run off the main thread and I setup a view controller I have the below snipped which runs on Android as expected, but on ios the UI remains on the initial state of
loading...
Copy code
class FakeVC {
    data class ViewState(val loading:Boolean = false, val content:String = "loading...")

    private val screenState = MutableStateFlow(ViewState())
    fun screenState() = screenState.asStateFlow()
    fun somFakeCall(){
        coroutineScope.launch {
            screenState.value = screenState.value.copy(loading = true)
            withContext(Dispatchers.Default){
                delay(3000)
            }
            screenState.value = screenState.value.copy(loading = false, content = "I am here after api call")
        }

    }

//... some clear logic for scope on dispose
}
and the mutated state data change is never seen on the ui , and here is the compose code which is
Copy code
val fvc = FakeVC()
MaterialTheme {
    LaunchedEffect(Unit){
        fvc.somFakeCall()
    }

    val viewState by l.screenState().collectAsState()

    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center){
        Text(viewState.content)
        if(viewState.loading){
            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center){
                CircularProgressIndicator(modifier = Modifier.align(Alignment.Center), color = Color.Red)
            }
        }
    }

    DisposableEffect(Unit){
        onDispose {
            fvc.clear()
        }
    }
}
this I tried it with latest coroutines and even with native-mt, is there anything else that may be required to setup it correctly when running it on iOS, as this works as expected on Android.
d
Hello! Can you please move code samples inside this thread 🧵 for easier reading of an overall #compose-ios channel?
I think in the code there is a mistake in this line:
Copy code
fun screenState() = screenState.asStateFlow()
h
I guess, this works, as I just want to expose it as ready only.
d
This function always creates a new StateFlow
h
it will create it only once, per class instantiation
d
Can you please create a reproducible sample on GitHub that will works on Android and fails on iOS?
If you want to expose it as StateFlow, you can just do this:
Copy code
fun screenState(): StateFlow<ViewState> = screenState
h
either way, it will be same thing, i tried this as well, but the same result, here is the code repository. https://github.com/carotkut94/sample
d
Thanks, you should change this line:
Copy code
val l = remember { FakeVC() }
h
🤦 yeah, this works, but i am surprised that it worked on Android, i guess the root composable was getting recomposed because of which it always had a new instance of the FakeVC in ios, but on android it was happening in a different way. Will dig deep, Thanks @Dima Avdeev
d
I don't know why it is working on Android. I think it is better to use remember on Android as well. Or save state holders in outside Comosable functions.