Skip to content

Commit d862367

Browse files
committed
fix: Cancel sensor collection jobs on disconnect or error
Ref: #8
1 parent 474b902 commit d862367

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

data/src/main/java/com/github/umercodez/sensorspot/data/sensorpublisher/SensorPublisher.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class SensorPublisher(
9595
}
9696
suspend fun connectAndPublish(mqttConfig: MqttConfig) = withContext(ioDispatcher){
9797

98+
sensorEventCollectionJob?.cancel()
99+
gpsDataCollectionJob?.cancel()
100+
98101
sensorEventCollectionJob = scope.launch {
99102
sensorEventProvider.events.collect { sensorEvent ->
100103

@@ -191,6 +194,9 @@ class SensorPublisher(
191194
}
192195
exception?.printStackTrace()
193196

197+
sensorEventCollectionJob?.cancel()
198+
gpsDataCollectionJob?.cancel()
199+
194200
}
195201

196202
})
@@ -200,6 +206,9 @@ class SensorPublisher(
200206
scope.launch {
201207
_mqttConnectionState.emit(MqttConnectionState.ConnectionError(e))
202208
}
209+
210+
sensorEventCollectionJob?.cancel()
211+
gpsDataCollectionJob?.cancel()
203212
}
204213

205214
}
@@ -230,6 +239,8 @@ class SensorPublisher(
230239
_mqttConnectionState.emit(MqttConnectionState.ConnectionError(exception))
231240
}
232241
exception?.printStackTrace()
242+
sensorEventCollectionJob?.cancel()
243+
gpsDataCollectionJob?.cancel()
233244
}
234245

235246
override fun messageArrived(topic: String?, message: MqttMessage?) {

0 commit comments

Comments
 (0)