question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] It seems impossible to add values to a WritableArray

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
dratwascommented, May 31, 2019

After investigation i found what is happening here. WritableNativeArray inherits from ReadableNativeArray. When you call pushInt on WritableNativeArray instance it will set this value in the Native array in https://github.com/facebook/react-native/blob/fc91bccd5333af88e49ecb19f56c5d6bb0931b1e/ReactAndroid/src/main/jni/react/jni/WritableNativeArray.cpp#L37

But 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:

  • Create WritableNativeArray - It copies empty array from native to java representation
  • Push Int to WritableNativeArray - It pushes Int to native array (local one is not updated)
  • Read Int from 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 every ReadableNativeArray instance.

So the working example :

ReadableNativeArray.setUseNativeAccessor(true);
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"

NOTE: It will not work on RN 0.60 since the setUseNativeAccessor flag is removed.

1reaction
dratwascommented, May 30, 2019

I’m able to reproduce it on 0.59.8 and 0.60.0-rc.0. Will take a look on it a bit later.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found