简体   繁体   English

ObjectAlreadyConsumedException:接收地图已被消耗

[英]ObjectAlreadyConsumedException: Receiving map already consumed

tl; tl; dr 博士
I try to get data of a WritableMap, in the same class and two times. 我尝试在同一个类中两次获取WritableMap的数据。 I get Receiving map already consumed error and I couldn't overcome that issue. 我收到的Receiving map already consumed错误,我无法解决该问题。


I've been developing a Native Module to use in my React-Native app. 我一直在开发要在我的React-Native应用程序中使用的Native模块。

I have 2 classes. 我有2节课。 One of them is MyModule class (is in MyModule.java file) and the other one is a Worker class (is in Worker.java file). 其中一个是MyModule类(位于MyModule.java文件中),另一个是Worker类(位于Worker.java文件中)。

In addition Worker class has another class its inside that is called Work class. 另外, Worker类在其内部还有另一个类,称为Work类。 Work class offers two methods to do works and to get works status. 工作类提供了两种进行工作和获取工作状态的方法。

Worker class has a hash table called as mWorks . Worker类具有一个称为mWorks的哈希表。 Worker class checks the incoming workName whether the work is already present or not in its constructor method. Worker类检查传入的workName作品的构造方法中是否已经存在该作品。 If the incoming work is already present, the work is assigned to public work variable. 如果输入的工作已经存在,则将工作分配给公共work变量。

When I try to call getState() and doWork() in my App (MyModule) I get the following error; 当我尝试在我的应用程序(MyModule)中调用getState()和doWork()时,出现以下错误;

E/unknown:React( 5578): Exception in native call from JS
E/unknown:React( 5578): com.facebook.react.bridge.ObjectAlreadyConsumedException: Receiving map already consumed
E/unknown:React( 5578):         at com.facebook.react.bridge.WritableNativeMap.putNativeMap(Native Method)
E/unknown:React( 5578):         at com.facebook.react.bridge.WritableNativeMap.putMap(WritableNativeMap.java:44)

I guess , when I call getState() method the map data field is consumed. 我猜想 ,当我调用getState()方法时,会消耗地图数据字段。 Then if I call doWork() I get the error. 然后,如果我调用doWork()则会收到错误消息。 Because I call mWorker.work.getState() in both of getState() and doWork() . 因为我在getState()doWork()中都调用mWorker.work.getState() doWork()

Is there any chance to overcome this exception? 有机会克服这个例外吗?

MyModule class have two methods as named getState() and doWork(). MyModule类具有两个名为getState()和doWork()的方法。

public class MyModule extends ReactContextBaseJavaModule {
    ...
    private Worker mWorker = null;
    ...
    public void getStatus(final String workName, final Callback successCallback) {
        mWorker = new Worker(workName);

        WritableMap response = new WritableNativeMap();
        response.putMap("workState", mWorker.work.getState());

        successCallback.invoke(response);
    }

    public void doWork(final String workName, final Callback successCallback) {
        mWorker = new Worker(workName);

        if(mWorker.work.doWork()) {
            successCallback.invoke(response);
            response.putMap("workState", mWorker.work.getState());
        } else {
            response.putMap("workState", mWorker.work.getState());
            response.putString("message", "An error has been occurred.");
            errorCallback.invoke(response);
        }

    }
}

Worker class has another class which is called Work. 工人阶级还有另一个阶级,叫做劳动。

public class Worker {
    ...
    public Work work;
    private Hashtable<String, Work> mWorks = new Hashtable<String, Work>();
    ...
    // CONSTRUCTOR
    public Worker () {
        Work work = null;

        if (this.mWorks.containsKey(pWorkName)) {
            Log.d(TAG, "Found an existing Work");
            work = this.mWorks.get(pWorkName);
        } else {
            Log.d(TAG, "Creating a new Work");
            work = new Work(pWorkName);
            this.mWorks.put(pWorkName, work);
        }
        this.work = work;
    }
    ...
    public class Work {
        ...
        public WritableMap getState()  {
            WritableMap result = new WritableNativeMap();

            if (this.isCompleted()) {
                result.putBoolean("Done", true);
            } else {
                result.putBoolean("Done", false);
            }

            return result;
        }

        public boolean doWork()  {
            // do something
            return aBoolValue;
        }
    }

}

The problem was response.putMap("workState", mWorker.work.getState()); 问题是response.putMap("workState", mWorker.work.getState()); line which is at the immediate after successCallback.invoke ! successCallback.invoke之后立即的successCallback.invoke

It had to before the callback invoke. 它必须在回调调用之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM