Skip to content

Commit ee8a0cf

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Fix ConcurrentModificationException while registering events
Summary: This diff fixes a ConcurrentModificationException that is thrown when registering events in React Native. This bug was introduced by D22483508 (80f1341), before event listeners were registered in the NativeModule Thread, now they are registered in the UI Thread. As part of this diff I change the type of mListeners variable to use CopyOnWriteArrayList instead of ArrayList because this variable is accessed from different threads. This will prevent the exception to happen, but additionally we need to verify if the change of threading made in D22483508 (80f1341) will cause any other issue (e.g. events not being delivered becuase the listeners are registered too late in the UI Thread) changelog:[Internal] Reviewed By: JoshuaGross Differential Revision: D22599747 fbshipit-source-id: 5c5e46710c4a559badbd713f536e6e6e464fda23
1 parent 980900c commit ee8a0cf

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Comparator;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.concurrent.CopyOnWriteArrayList;
2526
import java.util.concurrent.atomic.AtomicInteger;
2627

2728
/**
@@ -88,7 +89,8 @@ public int compare(Event lhs, Event rhs) {
8889
private final Map<String, Short> mEventNameToEventId = MapBuilder.newHashMap();
8990
private final DispatchEventsRunnable mDispatchEventsRunnable = new DispatchEventsRunnable();
9091
private final ArrayList<Event> mEventStaging = new ArrayList<>();
91-
private final ArrayList<EventDispatcherListener> mListeners = new ArrayList<>();
92+
private final CopyOnWriteArrayList<EventDispatcherListener> mListeners =
93+
new CopyOnWriteArrayList<>();
9294
private final List<BatchEventDispatchedListener> mPostEventDispatchListeners = new ArrayList<>();
9395
private final ScheduleDispatchFrameCallback mCurrentFrameCallback =
9496
new ScheduleDispatchFrameCallback();

0 commit comments

Comments
 (0)