为什么RN 0.55.4(Android)中的WritableArray.size()始终为1?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (193)

在Android中,有一个使用WritableArray的本机模块,如下所示:

public class UploadVideoModule extends ReactContextBaseJavaModule {
public WritableArray test=Arguments.createArray();
 UploadCallBack uploadCallBack = new UploadCallBack() {
        @Override
        public void testArray(int i) {
            test.pushString("aa "+i);
            System.out.println("================= wa test ============test:"+test+" ,size:"+test.size());
        } }

@Override
    public String getName() {
        return REACT_MODULE_NAME;
    }

    public UploadVideoModule(ReactApplicationContext reactContext) {
        super(reactContext);
        WritableArray wa=Arguments.createArray();
        wa.pushString("aaaaaaaaa");
        wa.pushString("bbbbbbbbb");
        wa.pushString("ccccccccc");
        Log.e(TAG,"======= wa ==========="+wa);
        Log.e(TAG,"======= wa ==========size:"+wa.size());
for (int i =0;i<5;i++){
            uploadCallBack.testArray(i);
        }
    }

 interface UploadCallBack {
        void testArray(int i);
    }
}

// ================log result ===================

E/tvb.react.uploadvideo.UploadVideoModule: ======= wa ===========["aaaaaaaaa","bbbbbbbbb","ccccccccc"] ======= wa ==========size:3 ================= wa test ============test:["aa 0"] ,size:1 ================= wa test ============test:["aa 0","aa 1"] ,size:1 ================= wa test ============test:["aa 0","aa 1","aa 2"] ,size:1 ================= wa test ============test:["aa 0","aa 1","aa 2","aa 3"] ,size:1 ================= wa test ============test:["aa 0","aa 1","aa 2","aa 3","aa 4"] ,size:1

these result are so strange!!!! Why wa 's size is right,but test is always 1 ? I found when i run with RN 0.51,all is normal ,and size is right! Unfortunately when run in RN 0.55.4, it's has problem.

Environment: OS: macOS 10.13.4 Node: v9.9.0 npm: 5.6.0 Watchman: 4.9.0 Android Studio: 3.1 Packages: (wanted => installed) "react": "16.3.2", "react-native": "0.55.4", // from 0.51 =>0.55.4

运行后出现以上奇怪的结果,该怎样解决呢?

提问于
用户回答回答于

确定条件:ReadableNativeArray.setUseNativeAccessor(true);

扫码关注云+社区

领取腾讯云代金券