ObjectAlreadyConsumedException:接收地图已经消耗 [英] ObjectAlreadyConsumedException: Receiving map already consumed

查看:114
本文介绍了ObjectAlreadyConsumedException:接收地图已经消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

tl; dr
I try to get data of a WritableMap, in the same class and two times. I get Receiving map already consumed error and I couldn't overcome that issue.

我一直在开发用于我的 React-Native 应用程序的 Native 模块.

I've been developing a Native Module to use in my React-Native app.

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

I have 2 classes. One of them is MyModule class (is in MyModule.java file) and the other one is a Worker class (is in Worker.java file).

另外Worker 类在其内部还有一个类,称为Work 类.Work 类提供了两种方法来做作品和获取作品状态.

In addition Worker class has another class its inside that is called Work class. Work class offers two methods to do works and to get works status.

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

Worker class has a hash table called as mWorks. Worker class checks the incoming workName whether the work is already present or not in its constructor method. If the incoming work is already present, the work is assigned to public work variable.

当我尝试在我的应用程序 (MyModule) 中调用 getState() 和 doWork() 时,出现以下错误;

When I try to call getState() and doWork() in my App (MyModule) I get the following error;

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() ,我会收到错误消息.因为我在 getState()doWork() 中都调用了 mWorker.work.getState().

I guess, when I call getState() method the map data field is consumed. Then if I call doWork() I get the error. Because I call mWorker.work.getState() in both of getState() and doWork().

有没有机会克服这个异常?

MyModule 类有两个名为 getState() 和 doWork() 的方法.

MyModule class have two methods as named getState() and 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 的类.

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;
        }
    }

}

推荐答案

问题出在 response.putMap("workState", mWorker.work.getState());successCallback.invoke!

它必须在回调调用之前.

It had to before the callback invoke.

这篇关于ObjectAlreadyConsumedException:接收地图已经消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆