How to run cordova plugins in the background?

前端 未结 1 2061
無奈伤痛
無奈伤痛 2020-12-08 10:57

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.\" S

相关标签:
1条回答
  • 2020-12-08 11:18

    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;
        }
    
    0 讨论(0)
提交回复
热议问题