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!
How to run cordova plugins in the background?
Asked Answered
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;
}
how? that is the native code of a plugin. I don't understand your question –
Kelt
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 –
Sander
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 –
Kelt
U mean to say if I want a plugin to run in background, I must edit that code and put that login in this? –
Sander
I 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. –
Kelt
@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). –
Rennie
This answer is not even close to the question –
Albertalberta
@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 –
Kelt
To run in Cordova Swift you need to add this:
commandDelegate.run(inBackground: { [self] in
callingMethod()
})
© 2022 - 2024 — McMap. All rights reserved.