@@ -25,6 +25,7 @@ import com.amplifyframework.storage.TransferState
2525import com.amplifyframework.storage.s3.AWSS3StoragePlugin
2626import com.amplifyframework.storage.s3.TransferOperations
2727import com.amplifyframework.storage.s3.transfer.worker.BaseTransferWorker
28+ import java.util.concurrent.ConcurrentHashMap
2829import java.util.concurrent.Executors
2930import kotlinx.coroutines.CoroutineScope
3031import kotlinx.coroutines.Dispatchers
@@ -49,6 +50,8 @@ internal class TransferWorkerObserver private constructor(
4950 AWSS3StoragePlugin .AWS_S3_STORAGE_LOG_NAMESPACE .format(this ::class .java.simpleName)
5051 )
5152
53+ private val observedTags = ConcurrentHashMap .newKeySet<String >()
54+
5255 init {
5356 attachObserverForPendingTransfer()
5457 }
@@ -195,13 +198,15 @@ internal class TransferWorkerObserver private constructor(
195198
196199 private suspend fun attachObserver (tag : String ) {
197200 withContext(Dispatchers .Main ) {
201+ if (! observedTags.add(tag)) return @withContext
198202 val liveData = workManager.getWorkInfosByTagLiveData(tag)
199203 liveData.observeForever(this @TransferWorkerObserver)
200204 }
201205 }
202206
203207 private suspend fun removeObserver (tag : String ) {
204208 withContext(Dispatchers .Main ) {
209+ if (! observedTags.remove(tag)) return @withContext
205210 workManager.getWorkInfosByTagLiveData(tag)
206211 .removeObserver(this @TransferWorkerObserver)
207212 }
0 commit comments