DienNT
DienNT

Reputation: 71

React Native callback type only permits a single invocation from native code

I'm using Promise to received callback from native module on React Native project, When I was connect to server to get data: * The first, I called action "LOGIN" * After received data from login I called action "GET_LIST",

But the response of action "GET_LIST", I received wrong as below:

09-06 08:48:22.117 13540-15635/? W/System.err: JNA: Callback com.lnzekesi.SoLibraryModule$1@8f35faa threw the following exception:
09-06 08:48:22.119 13540-15635/? W/System.err: java.lang.RuntimeException: Illegal callback invocation from native module. This callback type only permits a single invocation from native code.
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.facebook.react.bridge.CallbackImpl.invoke(CallbackImpl.java:30)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.facebook.react.bridge.PromiseImpl.resolve(PromiseImpl.java:32)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.lnzekesi.SharedLibraryModule$1.function_recv_msg(SharedLibraryModule.java:213)
09-06 08:48:22.125 13540-15635/? W/System.err:    at java.lang.reflect.Method.invoke(Native Method)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:520)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:551)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.Native.invokePointer(Native Method)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.Function.invokePointer(Function.java:490)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.Function.invoke(Function.java:434)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.Function.invoke(Function.java:354)
09-06 08:48:22.125 13540-15635/? W/System.err:    at com.sun.jna.Library$Handler.invoke(Library.java:244)
09-06 08:48:22.125 13540-15635/? W/System.err:    at java.lang.reflect.Proxy.invoke(Proxy.java:393)
09-06 08:48:22.125 13540-15635/? W/System.err:    at $Proxy2.cli_wait(Unknown Source)

This is my callback called from native module:

onMsg = new SoLib.fnCallback() {
    public synchronized void recvMsg(Pointer cli, final Pointer msg) {
        promise.resolve(getResponse(msg, resIndex));
        //always delete recv msg
        SoLib.msgDelete(msg);
    }
};

SoLib.cliRegOnMsg(cli, onMsg);
SoLib.cliConnect(cli);

Additional Information

Upvotes: 5

Views: 14188

Answers (1)

yedidyak
yedidyak

Reputation: 1984

A ReactNative Promise object, like a JavaScript Promise, can only be called once. If you want to send something multiple times from a native module to JS, you should be using an event pattern using something like DeviceEventEmiter android, ios.

Upvotes: 9

Related Questions