@@ -31,6 +31,7 @@ import kotlinx.coroutines.Dispatchers
31
31
import kotlinx.coroutines.asCoroutineDispatcher
32
32
import kotlinx.coroutines.launch
33
33
import kotlinx.coroutines.withContext
34
+ import java.util.concurrent.ConcurrentHashMap
34
35
35
36
internal class TransferWorkerObserver private constructor(
36
37
context : Context ,
@@ -49,6 +50,9 @@ internal class TransferWorkerObserver private constructor(
49
50
AWSS3StoragePlugin .AWS_S3_STORAGE_LOG_NAMESPACE .format(this ::class .java.simpleName)
50
51
)
51
52
53
+ private val observedTags =
54
+ ConcurrentHashMap .newKeySet<String >()
55
+
52
56
init {
53
57
attachObserverForPendingTransfer()
54
58
}
@@ -195,15 +199,18 @@ internal class TransferWorkerObserver private constructor(
195
199
196
200
private suspend fun attachObserver (tag : String ) {
197
201
withContext(Dispatchers .Main ) {
202
+ if (observedTags.contains(tag)) return @withContext
198
203
val liveData = workManager.getWorkInfosByTagLiveData(tag)
199
204
liveData.observeForever(this @TransferWorkerObserver)
200
205
}
201
206
}
202
207
203
208
private suspend fun removeObserver (tag : String ) {
204
209
withContext(Dispatchers .Main ) {
210
+ if (! observedTags.contains(tag)) return @withContext
205
211
workManager.getWorkInfosByTagLiveData(tag)
206
212
.removeObserver(this @TransferWorkerObserver)
213
+ observedTags.remove(tag)
207
214
}
208
215
}
209
216
}
0 commit comments