THREAD WARNING: exec() call blocked the main thread. Plugin should use CordovaInterface.getThreadPool(). – Cordova Plugin Warning

Hey all,

So I came across this today. My plugin needed to add and manipulate views within the Cordova app. The problem is that you can’t do this since in Cordova, the WebView is on the WebCore thread and it can manipulate the UIThread directly. Hence you get this nice error/warning:

THREAD WARNING: exec() call to [pluginname].[method] blocked the main thread for XXms. Plugin should use CordovaInterface.getThreadPool().

The way to fix this is to use the runOnUiThread method from your plugin’s source file. To do this you need to get the main activity of the app. Here’s how you would do that:

[sourcecode language=”java”]

this.cordova.getActivity();

[/sourcecode]

From here you would then invoke the runOnUiThread method and pass in a new Runnable object with its own run method. Within the run method is where you would put your code that changes your views. Here’s a snippet to tie it all together:

[sourcecode language=”java”]

public boolean execute(String action, final JSONArray inputs, final CallbackContext callbackContext) throws JSONException {

PluginResult result = null;

if (action.equals(“myPluginMethod”)) {

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

public void run() {

callbackContext.sendPluginResult(myMethod(inputs));

}

});

result = new PluginResult(Status.OK);

}

}

[/sourcecode]

Hope this helps! Feel free to share your experiences in the comments.

Don Marges

Don Marges

Pragmatic Software Developer

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin