Im making an application based on phonegap (cordova). I have tested it some times, and lately I saw a message in xcode that said "Plugin should use a background thread." So is it possible to make cordova plugins run in the background of the app? if so, please tell how. Thanks!
Asked
Active
Viewed 2.6k times
2 Answers
34
A background thread isn't the same that executing code while the app is in background, a background thread is used to don't block the UI while you execute a long task.
Example of background thread on iOS
- (void)myPluginMethod:(CDVInvokedUrlCommand*)command
{
// Check command.arguments here.
[self.commandDelegate runInBackground:^{
NSString* payload = nil;
// Some blocking logic...
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload];
// The sendPluginResult method is thread-safe.
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
Example of background thread on android
@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;
}

jcesarmobile
- 51,328
- 11
- 132
- 176
-
how? that is the native code of a plugin. I don't understand your question – jcesarmobile Feb 26 '15 at 19:29
-
Yeah I know that is native code, that must be in .m file. My question is, how to use this function if I want to run specific plugin in background – developer Feb 27 '15 at 11:40
-
it's not for a specific plugin in background, it's for your own plugin, you can't run other plugins on background unless they have this code or you fork the plugin and create your own version with this background code – jcesarmobile Feb 27 '15 at 11:44
-
U mean to say if I want a plugin to run in background, I must edit that code and put that login in this? – developer Feb 27 '15 at 12:20
-
1I mean, this is the native code to run in background. If you want your plugin to run in background it needs the runInBackground (iOS) or cordova.getThreadPool().execute(new Runnable() (android) part. If the plugin isn't yours, then the author should add this code (if he wants) or you fork the plugin and add this changes. – jcesarmobile Feb 27 '15 at 12:24
-
@developer, actually yes, you can just edit code of the plugin to make it work. But be aware that on the plugin update your changes will be cleared. So for saving this code you need to do like jcesarmobile said (ask plugin owner to make those changes or fork). – extempl Jul 22 '15 at 04:28
-
1This answer is not even close to the question – Varun Kumar Aug 20 '18 at 00:20
-
@VarunKumar, do you think an accepted answer with 22 upvotes is not even close to the question? The answer is good. You and the other downvoter are looking for something different than what is being asked here. It’s not to run plugins when the device is in background, is to run plugin code in a background thread to not block the UI while the code is executed – jcesarmobile Aug 20 '18 at 08:52
0
To run in Cordova Swift you need to add this:
commandDelegate.run(inBackground: { [self] in
callingMethod()
})

Leandro Ariel
- 727
- 8
- 5