reactormonk
05/23/2024, 9:28 AMJob
to the parent context?
val connectionJob = Job(viewModelScope.coroutineContext.job)
Sam
05/23/2024, 9:31 AMJob
isn't something you should need to do much, and it's very easy to get wrong. What's stopping you from just making a job with launch
?bezrukov
05/23/2024, 9:31 AMval connectionJob = viewModelScope.launch { }
reactormonk
05/23/2024, 9:33 AMlaunch
, nest them inside?Sam
05/23/2024, 9:34 AMreactormonk
05/23/2024, 9:35 AMlateinit var connectionJob: Job
connectionJob = viewModelScope.launch {
launch {
connection.state.collect {
bluetoothConnectionState.value = it
when (it?.state) {
GattConnectionState.STATE_DISCONNECTED -> {
connectionJob.cancel()
}
else -> {}
}
}
}
launch {
connection.networkState.collect {
networkingStateFlow.value = it
}
}
}
reactormonk
05/23/2024, 9:36 AMlaunch
were wrongSam
05/23/2024, 9:36 AMviewModelScope
part. The launch
block creates its own scope, and you want your inner jobs to be children of that scope, instead of the outer scope.reactormonk
05/23/2024, 9:37 AMlateinit var
reactormonk
05/23/2024, 9:37 AMDmitry Khalanskiy [JB]
05/23/2024, 10:08 AMreactormonk
05/23/2024, 1:49 PMconnectionJob
isn't properly wired in, gotta go with the Job
Dmitry Khalanskiy [JB]
05/23/2024, 1:53 PMviewModelScope.launch {}
.reactormonk
05/23/2024, 1:53 PMconnectionJob
will be null
inside though, so that's not gonna work out too wellDmitry Khalanskiy [JB]
05/23/2024, 1:55 PMconnectionJob
inside the launch
?reactormonk
05/23/2024, 1:55 PMDmitry Khalanskiy [JB]
05/23/2024, 1:56 PMDmitry Khalanskiy [JB]
05/23/2024, 1:56 PMviewModelScope.launch {
val connectionScope = this
launch {
connection.state.collect {
bluetoothConnectionState.value = it
when (it?.state) {
GattConnectionState.STATE_DISCONNECTED -> {
connectionScope.cancel()
}
else -> {}
}
}
}
launch {
connection.networkState.collect {
networkingStateFlow.value = it
}
}
}
reactormonk
05/23/2024, 1:56 PMreactormonk
05/23/2024, 1:57 PMJob()
usages ^^