Colton Idle
05/01/2025, 6:40 PM@Composable
fun App() {
LaunchedEffect(Unit) {
applicationScope.launch {
connection?.isConnected?.collectLatest {
Logger.i { "connected? $it" }
} } }
no logs
Option 2
@Composable
fun App() {
applicationScope.launch {
connection?.isConnected?.collectLatest {
Logger.i { "connected? $it" }
} } }
I get double logsjw
05/01/2025, 6:47 PMjw
05/01/2025, 6:47 PMjw
05/01/2025, 6:48 PMSeri
05/01/2025, 6:48 PMconnection?.isConnected?.
) on the first frame of compositionColton Idle
05/01/2025, 6:49 PMinit {}
block of App.kt?Landry Norris
05/01/2025, 6:55 PMjw
05/01/2025, 6:55 PMColton Idle
05/01/2025, 6:57 PMvar connection by mutableStateOf<Connection?>(null)
jw
05/01/2025, 6:58 PMisConnected
valueColton Idle
05/01/2025, 6:59 PM@Composable
fun App() {
LaunchedEffect(Unit) {
snapshotFlow { connection }
.collectLatest { conn ->
conn?.isConnected?.collectLatest {
Logger.i { "connected? $it" }
}
}
}
Colton Idle
05/01/2025, 7:00 PMColton Idle
05/01/2025, 7:05 PMval connected = connection?.isConnected?.collectAsState()
jw
05/01/2025, 7:06 PMisConnected
returns the same instance each time it's calledjw
05/01/2025, 7:06 PMColton Idle
05/01/2025, 7:09 PMvar connection by mutableStateOf<Connection?>(null)
and then
class Connection {
private val _isConnected = MutableStateFlow(false)
val isConnected = _isConnected.asStateFlow()
Colton Idle
05/01/2025, 7:09 PMjw
05/01/2025, 7:10 PMisConnected
is a val
without a get()
that you're good.jw
05/01/2025, 7:10 PMval isConnected: StateFlow<Boolean> get() = _isConnected
jw
05/01/2025, 7:10 PMasStateFlow()
is almost always pointlessColton Idle
05/01/2025, 7:12 PMColton Idle
05/01/2025, 7:14 PMHowever, I would do val isConnected: StateFlow<Boolean> get() = _isConnectedwouldn't that mean that now the consumer can edit this?
jw
05/01/2025, 7:15 PMStateFlow
is not writableColton Idle
05/01/2025, 7:16 PMColton Idle
05/01/2025, 7:16 PMColton Idle
05/01/2025, 7:16 PMColton Idle
05/01/2025, 7:16 PM