diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java index 0b787014524b06..3dae1f0ba165d8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java @@ -39,15 +39,21 @@ public static int getJNIPassCounter() { return mJniCallCounter; } - private HashMap getLocalMap() { - if (mLocalMap != null) { - return mLocalMap; - } + private void ensureKeysAreImported() { synchronized (this) { if (mKeys == null) { mKeys = Assertions.assertNotNull(importKeys()); mJniCallCounter++; } + } + } + + private HashMap getLocalMap() { + if (mLocalMap != null) { + return mLocalMap; + } + ensureKeysAreImported(); + synchronized (this) { if (mLocalMap == null) { Object[] values = Assertions.assertNotNull(importValues()); mJniCallCounter++; @@ -69,11 +75,8 @@ private HashMap getLocalMap() { if (mLocalTypeMap != null) { return mLocalTypeMap; } + ensureKeysAreImported(); synchronized (this) { - if (mKeys == null) { - mKeys = Assertions.assertNotNull(importKeys()); - mJniCallCounter++; - } // check that no other thread has already updated if (mLocalTypeMap == null) { Object[] types = Assertions.assertNotNull(importTypes()); @@ -187,48 +190,47 @@ public int getInt(@NonNull String name) { @Override public @NonNull Iterator> getEntryIterator() { - if (mKeys == null) { - mKeys = Assertions.assertNotNull(importKeys()); - } + ensureKeysAreImported(); final String[] iteratorKeys = mKeys; - final Object[] iteratorValues = Assertions.assertNotNull(importValues()); - return new Iterator>() { - int currentIndex = 0; + synchronized (this) { + final Object[] iteratorValues = Assertions.assertNotNull(importValues()); - @Override - public boolean hasNext() { - return currentIndex < iteratorKeys.length; - } + return new Iterator>() { + int currentIndex = 0; - @Override - public Map.Entry next() { - final int index = currentIndex++; - return new Map.Entry() { - @Override - public String getKey() { - return iteratorKeys[index]; - } - - @Override - public Object getValue() { - return iteratorValues[index]; - } - - @Override - public Object setValue(Object value) { - throw new UnsupportedOperationException( - "Can't set a value while iterating over a ReadableNativeMap"); - } - }; - } - }; + @Override + public boolean hasNext() { + return currentIndex < iteratorKeys.length; + } + + @Override + public Map.Entry next() { + final int index = currentIndex++; + return new Map.Entry() { + @Override + public String getKey() { + return iteratorKeys[index]; + } + + @Override + public Object getValue() { + return iteratorValues[index]; + } + + @Override + public Object setValue(Object value) { + throw new UnsupportedOperationException( + "Can't set a value while iterating over a ReadableNativeMap"); + } + }; + } + }; + } } @Override public @NonNull ReadableMapKeySetIterator keySetIterator() { - if (mKeys == null) { - mKeys = Assertions.assertNotNull(importKeys()); - } + ensureKeysAreImported(); final String[] iteratorKeys = mKeys; return new ReadableMapKeySetIterator() { int currentIndex = 0;