ajaylouis
ajaylouis

Reputation: 247

how to implement thread process in phonegap development?

I am developing phonegap application that reads json data from server but it stops the execution of current process untill the javascript got executed.

i want to execute the process like thread.

how can i achieve it?

Upvotes: 2

Views: 6214

Answers (1)

MAST3RMIND
MAST3RMIND

Reputation: 592

JavaScript in the WebView does not run on the UI thread. It runs on the WebCore thread. The execute method also runs on the WebCore thread.

If you need to interact with the UI, you should use the following:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        final long duration = args.getLong(0);
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                ...
                callbackContext.success(); // Thread-safe.
            }
        });
        return true;
    }
    return false;
}

If you do not need to run on the UI thread, but do not want to block the WebCore thread:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        final long duration = args.getLong(0);
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                ...
                callbackContext.success(); // Thread-safe.
            }
        });
        return true;
    }
    return false;
}

Upvotes: 3

Related Questions