Skip to content

Commit 6ec24e7

Browse files
committed
fix: Properly cancel coroutines on service
Introduced specific `Job` instances for MQTT connection state, selected sensors, and GPS selection state coroutines. This ensures that all running coroutines are explicitly cancelled when `stopPublishing()` is called, preventing potential leaks and unintended background processing. ref #1
1 parent fa9a416 commit 6ec24e7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

data/src/main/java/com/github/umercodez/sensorspot/data/sensorpublisherservice/SensorPublisherServiceImp.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import com.github.umercodez.sensorspot.data.sensorpublisher.SensorPublisher
4444
import dagger.hilt.android.AndroidEntryPoint
4545
import kotlinx.coroutines.CoroutineScope
4646
import kotlinx.coroutines.Dispatchers
47+
import kotlinx.coroutines.Job
4748
import kotlinx.coroutines.SupervisorJob
4849
import kotlinx.coroutines.cancel
4950
import kotlinx.coroutines.flow.SharedFlow
@@ -62,6 +63,9 @@ class SensorPublisherServiceImp : Service(), SensorPublisherService {
6263

6364
private lateinit var sensorPublisher: SensorPublisher
6465
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
66+
private var mqttConnectionStateJob: Job? = null
67+
private var selectedSensorsJob: Job? = null
68+
private var gpsSelectionStateJob: Job? = null
6569

6670

6771
override val mqttConnectionState: SharedFlow<MqttConnectionState>
@@ -109,7 +113,7 @@ class SensorPublisherServiceImp : Service(), SensorPublisherService {
109113
val mqttConfig = MqttConfig.fromSettings(settings)
110114

111115

112-
scope.launch {
116+
mqttConnectionStateJob = scope.launch {
113117
sensorPublisher.mqttConnectionState.collect{ connectionState ->
114118
Log.d(TAG, "connection State: $connectionState")
115119
if(connectionState == MqttConnectionState.Connected){
@@ -160,13 +164,13 @@ class SensorPublisherServiceImp : Service(), SensorPublisherService {
160164
sensorPublisher.sensorSamplingRate = settings.sensorSamplingRate
161165
sensorPublisher.connectAndPublish(mqttConfig)
162166

163-
scope.launch {
167+
selectedSensorsJob = scope.launch {
164168
sensorsRepository.getSelectedSensorsAsFlow().collect { selectedSenors ->
165169
sensorPublisher.sensorIntTypes = selectedSenors.map { it.type }
166170
}
167171
}
168172

169-
scope.launch {
173+
gpsSelectionStateJob = scope.launch {
170174
sensorsRepository.gpsSelectionState.collect { isGpsSelected ->
171175

172176
if(!locationPermissionGranted)
@@ -185,8 +189,13 @@ class SensorPublisherServiceImp : Service(), SensorPublisherService {
185189
override fun stopPublishing() {
186190
scope.launch {
187191
sensorPublisher.disconnect()
192+
isConnected = false
188193
}
189194

195+
mqttConnectionStateJob?.cancel()
196+
selectedSensorsJob?.cancel()
197+
gpsSelectionStateJob?.cancel()
198+
190199
stopSelf()
191200
}
192201

0 commit comments

Comments
 (0)