0

My code doesn't work properly and I can't find where the problem is.

 // MutableStateFlow create
// TestService.kt
val flowState = MutableStateFlow(true)

class TestService : Service() {
    override fun onBind(p0: Intent?) = null

    private val handler = CoroutineExceptionHandler { _, e ->
        println("thread exception -> ${e.message}")
    }

    private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob() + handler)


    override fun onCreate() {
        super.onCreate()
        scope.launch(Dispatchers.Main) { flowState.emit(Random.nextBoolean()) }

        object : Thread() {
            override fun run() {
                super.run()
                scope.launch {
                    repeat(50) {
                        val state = false // Random.nextBoolean()
                        scope.launch(Dispatchers.Main) { flowState.emit(state) }
                        Log.d("sky", "run: Service state -> $state")
                        delay(1000)
                    }
                }
                scope.launch {
                    repeat(50) {
                        val state = false // Random.nextBoolean()
                        scope.launch { flowState.emit(state) }
                        Log.d("sky", "run2: Service state[IO] -> $state")
                        delay(1000)
                    }
                }
                scope.launch {
                    repeat(50) {
                        val state = false // Random.nextBoolean()
                        scope.launch(Dispatchers.Default) { flowState.emit(state) }
                        Log.d("sky", "run2: Service state[Default] -> $state")
                        delay(1000)
                    }
                }
            }
        }.start()
    }
}
// end TestService.kt

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DemoTheme {
                val context = LocalContext.current
                val state = flowState.collectAsState()
                Log.d("sky", "onCreate: state -> ${state.value}")
                context.startService(Intent(context, TestService::class.java))
                LaunchedEffect(Unit) {
                    flowState.collectLatest {
                        Log.d("sky", "onCreate: state - > $it")
                    }
                }
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Text(
                        text = "Hello World! flow -> ${state.value}",
                    )
                }
            }
        }
    }
}

I created a MutableStateFlow variable globally, TestService emit it, and then in Compose I retrieve the value of MutableStateFlow. I use collectAsState to retrieve the value, but Compose does not retrieve it and then recompose; I also tried using collect to collect MutableStateFlow but did not get the value.

Thanks.

0 Answers0