Taron Sargsyan Taron Sargsyan - 3 months ago 32
iOS Question

PhoneGap native plugin lifecycle

How is phonegap native plugin lifecycle for android and ios?

Imagine that I have java/ObjectiveC code which is doing some work for my phonegap app, however in my plugin i start several threads to support small part of the main work. So what happens after callbackContext.success(); does environment still exists where my threads still running or my VM will be stopped after result received by phonegap???

Thanks in advance.

Answer

The plugins are alive as long as the webview is alive, but plugins by default, will only trigger the callback once

If your plugin is initialized and you want it to keep it sending success or error callbacks to the javascript side you have to use the setKeepCallback(true); option. To do so, you will need to have a PluginResult in your plugin class and then, use the callbackContext.sendPluginResult() instead of the callbackContext.success()

something like this:

private PluginResult pgResult= null;
private CallbackContext cbContext;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if ("test".equals(action)) {
        //Do something here
        cbContext = callbackContext;
        pgResult = new PluginResult(PluginResult.Status.OK);
        pgResult.setKeepCallback(true);
        cbContext.sendPluginResult(pgResult);
        return true;
    }
    return false;  // Returning false results in a "MethodNotFound" error.
}

//This method can be called when you receive native events or something like that
public void anotherMethod() {
    //Here you can continue calling the javascript callbacks
    pgResult = new PluginResult(PluginResult.Status.OK);
    pgResult.setKeepCallback(true);
    cbContext.sendPluginResult(pgResult);
}