[Android] It seems impossible to add values to a WritableArray
See original GitHub issueWhen I add a value to a WritableArray (for example, using pushInt(1) or pushMap(myMap)), it has no effect. The size of the array is still 0, and if I try to access the data I pushed, it triggers and out of range exception.
React Native version: 0.59.2 React Native Environment Info: System: OS: Windows 10 CPU: (8) x64 Intel® Core™ i7-7700HQ CPU @ 2.80GHz Memory: 3.22 GB / 15.86 GB Binaries: Yarn: 1.7.0 - C:\Users\Mickael-AccoPilot\AppData\Roaming\npm\yarn.CMD npm: 5.6.0 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: Version 3.3.0.0 AI-182.5107.16.33.5264788
Steps To Reproduce
This code triggers the bug every time:
WritableArray test_array = Arguments.createArray();
Log.d("RNTest", "test size " + test_array.size()); // => "test size 0"
test_array.pushInt(1);
Log.d("RNTest", "test size " + test_array.size()); // => "test size 0"
Log.d("RNTest", "test int " + test_array.getInt(0)); // => exception "length=0; index=0"
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
elements not adding to ArrayList Java - Android - Stack Overflow
I see two things which might be your problem. comparing strings in java should be done with the String.equals() method, not the ==...
Read more >Android Native Modules
In the following guide you will create a native module, CalendarModule , that will allow you to access Android's calendar APIs from ...
Read more >error: package com.facebook.react.bridge does not exist
I'm trying to run gradle build in the android folder of my React Native app but I keep getting this strange error. >...
Read more >React Native Share Extension - Medium
Hi Everyone! So while working on my react native project in Sankey Solutions I encountered an issue which seemed to be an impossible...
Read more >React Native with JNI & C - The BHW Group
Via its Java bridge and JNI, we can run C code on Android (iOS will be covered in ... Add your package to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After investigation i found what is happening here.
WritableNativeArray
inherits fromReadableNativeArray
. When you callpushInt
onWritableNativeArray
instance it will set this value in theNative
array in https://github.com/facebook/react-native/blob/fc91bccd5333af88e49ecb19f56c5d6bb0931b1e/ReactAndroid/src/main/jni/react/jni/WritableNativeArray.cpp#L37But when you want to read something from this array, it takes the value from local copy in https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java#L96 which is copied only once while creating instance.
So basically the flow looks like:
WritableNativeArray
- It copies empty array fromnative
tojava
representationWritableNativeArray
- It pushes Int tonative
array (local one is not updated)WritableNativeArray
- It tries to read value from local array which is still empty because it is synchronised only once.If you use RN 0.59.8 you could use
ReadableNativeArray.setUseNativeAccessor(true)
to fix it, but keep in mind that it will use the native array in everyReadableNativeArray
instance.So the working example :
NOTE: It will not work on RN
0.60
since thesetUseNativeAccessor
flag is removed.I’m able to reproduce it on
0.59.8
and0.60.0-rc.0
. Will take a look on it a bit later.