Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova plugin blocking thread

I'm creating a custom plugin for an Android cordova/phonegap application, and the native Java side fire's up an activity that includes a callback called by a service it starts. The idea is that callback gets hit every second or so from the service and works great, but the problem is that I can't seem to get this running in another thread, so the main cordova thread is blocked and the app is totally unresponsive.

Based on the documentation I'm doing this:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext context) throws JSONException
{

/* snip */
    cordova.getThreadPool().execute(new Runnable()
    {
        public void run()
        {
            Intent myIntent = new Intent(this.cordova.getActivity(), myMonitoring.class);
            this.cordova.getActivity().startActivity(myIntent);    
            callbackContext.success();
        }
    });

    return true;
}

I realise here the JS callback will never get called (i.e. callbackContext.success();) because the activity is blocking, but shouldn't the actual phonegap thread keep running after return true? If I remove the startActivity call then the app carries on working as expected.

like image 933
Matt Lacey Avatar asked Nov 14 '13 20:11

Matt Lacey


1 Answers

I'm investigating a problem (another). But it seems that UI Main thread is not equal to Cordova thread. And what happens is that the UI Main thread is indeed released, but the Cordova thread is not.

BTW, I also think that there is a difference between:

callbackContext.success(); 

and

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));

It is still not baked, sorry. If I'll find some more, I'll update.

Also, you can call callbackContext.success(); first, and then the rest.. (if it helps in anything).

like image 135
Oren Avatar answered Nov 17 '22 11:11

Oren