Skip to content

Commit

Permalink
Fix Concurrency issue by using CopyOnWriteArrayList for observers
Browse files Browse the repository at this point in the history
Replaces the observers ArrayList with CopyOnWriteArrayList in ProductImageMap to prevent ConcurrentModificationException during concurrent access. This change ensures thread-safe modifications and iterations over the observer list, improving the stability of the product image fetching mechanism.
  • Loading branch information
Joel Dean committed Feb 23, 2024
1 parent 1567cce commit 17d27a8
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.withContext
import org.wordpress.android.fluxc.store.WCProductStore
import org.wordpress.android.fluxc.store.WCProductStore.FetchSingleProductPayload
import java.lang.ref.WeakReference
import java.util.concurrent.CopyOnWriteArrayList
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -27,7 +28,7 @@ class ProductImageMap @Inject constructor(
fun onProductFetched(remoteProductId: Long)
}

private val observers: MutableList<WeakReference<OnProductFetchedListener>> = mutableListOf()
private val observers: MutableList<WeakReference<OnProductFetchedListener>> = CopyOnWriteArrayList()

private val map by lazy {
HashMap<Long, String>()
Expand Down

0 comments on commit 17d27a8

Please sign in to comment.