ObjectAlreadyConsumedException:接收已消耗的地图

时间:2016-05-12 12:34:51

标签: java android react-native

TL;博士
我尝试在同一个类中获取WritableMap的数据两次。我收到Receiving map already consumed错误,我无法解决这个问题。

我一直在开发一个在我的React-Native应用程序中使用的本机模块。

我有2节课。其中一个是MyModule类(在MyModule.java文件中),另一个是Worker类(在Worker.java文件中)。

此外,Worker类还有另一个类,其内部称为Work类。工作班提供两种方法来完成工作并获得工作状态。

Worker类有一个名为mWorks的哈希表。 Worker类在其构造方法中检查传入的workName工作是否已存在。如果传入的工作已经存在,则将工作分配给公共work变量。

当我尝试在我的App(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)

我想,当我调用getState()方法时,会消耗地图数据字段。然后,如果我拨打doWork(),我就会收到错误消息。因为我在mWorker.work.getState()getState()中都会调用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类有另一个名为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;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

问题是response.putMap("workState", mWorker.work.getState());行,它位于successCallback.invoke之后的最后一行!

必须在回调调用之前。