Cordova Exec blocked the main thread even after using CordovaInterface.getThreadPool ()

I get the following warning:

THREAD WARNING: exec()
 call to MyPlugin.setAndroidPreferences blocked the main thread for 49ms.   
Plugin should use CordovaInterface.getThreadPool().

But from my code, I use cordova.getThreadPool():

private boolean setAndroidPreferences(
        final JSONArray args,
        final CallbackContext callbackContext)
{
    cordova.getThreadPool().execute(new Runnable() {
        @Override
        public void run() {
            try {
                /* ... */

                if ( /* ... */) 
                {
                    final SharedPreferences settings = cordova.getActivity().getSharedPreferences(preferenceLib, Context.MODE_PRIVATE);
                    final SharedPreferences.Editor editor = settings.edit();

                    editor.putString(preferenceName, preferenceValue);
                    editor.commit();
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));                     
                } else {                                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e(TAG, "getSetSharePreferences" + ": Error: " + PluginResult.Status.JSON_EXCEPTION);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
            }
        }
    });
    return true;
}

What am I doing wrong?

Thank,

+5
source share
3 answers

According to THREAD WARNING: calling exec () blocked the main thread. The plugin should use CordovaInterface.getThreadPool (). - Cordova plugin warning , try the following:

    public boolean execute(String action, final JSONArray inputs, final CallbackContext callbackContext) throws JSONException {
        if (action.equals("setAndroidPreferences")) {
            cordova.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    callbackContext.sendPluginResult(setAndroidPreferences(inputs));
                }
            });
        }
    }

    private PluginResult setAndroidPreferences(JSONArray args) {
        try {
            if ( /* ... */) {
                SharedPreferences settings = cordova.getActivity().getSharedPreferences(preferenceLib, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();

                editor.putString(preferenceName, preferenceValue);
                editor.commit();
                return new PluginResult(PluginResult.Status.OK);                     
            } else {
                return new PluginResult(PluginResult.Status.ERROR);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e(TAG, "getSetSharePreferences" + ": Error: " + PluginResult.Status.JSON_EXCEPTION);
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }
}
+6
source

You should use:

this.cordova.getActivity().runOnUiThread(new Runnable() {...}

instead:

cordova.getThreadPool().execute(new Runnable() {...}
0
source

, ,   50 .  OCI SDK ,   , ExecutorTask .   connect() , .   OCI (Java VM) .     .   .

0

Source: https://habr.com/ru/post/1547255/


All Articles