What I’m trying to do:
- user fills out a form data, clicks submit.
- user presented with a loading screen. (HTML element).
- Application makes asynchronous call, PhoneGap plugin, which saves this data in db. That’s where the problem is, because the call is synchronous instead.
- When html app receives a callback, I hide loading screen.
Because of synchronous call that’s what I got:
- user fills out a form, submits
- HTML app freezes, data is being saved to a database.
- loading screen appears
- callback is called, a few milliseconds after.
Here’s some demo (trimmed) code:
Java:
public class SomePlugin extends Plugin
...
public PluginResult execute(String action, JSONArray data, String callbackId)
{
PluginResult result = null;
//
// save data in the background...
//
Log.d("TAG", "Some Message...");
result = new PluginResult(Status.OK, "");
// or
// result = new PluginResult(Status.ERROR);
return result;
}
...
public boolean isSynch(String action) {
return false; // always do async...
}
JavaScript:
$('#loading-screen').show();
var successCallback = function() {
console.log('Success Callback');
$('#loading-screen').hide();
};
var failureCallback = function() {
console.log('Failed Callback');
$('#loading-screen').hide();
};
PhoneGap.exec(successCallback, failureCallback, 'PluginName', 'actionName', data);
From PhoneGap source:
* Execute a PhoneGap command. It is up to the native side whether this action is synch or async.
* The native side can return:
* Synchronous: PluginResult object as a JSON string
* Asynchrounous: Empty string ""
* If async, the native side will PhoneGap.callbackSuccess or PhoneGap.callbackError,
* depending upon the result of the action.
So I thought maybe this line is incorrect in that case:
new PluginResult(Status.OK, "");
Note: If wrap (JavaScript) PhoneGap.exec call in setTimeout (with a delay of 1 sec for example), loading screen will work “properly” (it’s still frozen but user have an instant feedback), but that’s obviously not a solution.
I think I just not seeing something obvious here, just one parameter or something somewhere.
Thanks.
I think its perfect for an AsyncTask
just process you dbStorage in doInBackground and handle finishing in onPostExcecute().
You are free to update status in onProgressUpdate