我正在做一个基于phonegap(cordova)的应用程序。我已经测试过几次了,最近我在xcode中看到一条消息,说“插件应该使用后台线程”。那么有没有可能让cordova插件在应用程序的后台运行?如果有,请告诉我怎么做。谢谢!
pwuypxnk1#
后台线程与应用在后台执行代码不同,后台线程用于在执行长任务时不阻塞UI。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]; }]; }
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; }
toiithl62#
要在Cordova Swift中运行,您需要添加以下内容:
commandDelegate.run(inBackground: { [self] in callingMethod() })
2条答案
按热度按时间pwuypxnk1#
后台线程与应用在后台执行代码不同,后台线程用于在执行长任务时不阻塞UI。
iOS上的后台线程示例
Android上的后台线程示例
toiithl62#
要在Cordova Swift中运行,您需要添加以下内容: