如何让PhoneGap IOS插件在后台线程中运行

Posted

技术标签:

【中文标题】如何让PhoneGap IOS插件在后台线程中运行【英文标题】:How do you make PhoneGap IOS plugins run in a background thread 【发布时间】:2014-10-23 13:40:16 【问题描述】:

在 xCode 6.1 中,为 ios 运行 Cordova 3.6.0 我收到警告说插件应该在后台线程中运行。

我的问题是如何让 File、Camera、NetworkStatus、StatusBar、Notification 和 EmailComposer 插件在后台线程中运行?

我将不胜感激。下面是我在 xCode 控制台中得到的输出。

2014-10-23 06:56:13.683 BackTracker[1689:462317] Multi-tasking -> Device: YES, App: YES 

2014-10-23 06:56:13.726 BackTracker[1689:462317] Unlimited access to network resources 

2014-10-23 06:56:13.739 BackTracker[1689:462317] Started backup to iCloud! Please be careful. Your application might rejected by Apple if you store too much data. For more information please read "iOS Data Storage Guidelines" You could find it at the following address https://developer.apple.com/icloud/documentation/data-storage/ .

2014-10-23 06:56:14.002 BackTracker[1689:462317] [CDVTimer][file] 78.798950ms 

2014-10-23 06:56:14.137 BackTracker[1689:462317] [CDVTimer][splashscreen] 133.718014ms

2014-10-23 06:56:14.147 BackTracker[1689:462317] [CDVTimer][statusbar] 9.751022ms

2014-10-23 06:56:14.148 BackTracker[1689:462317] [CDVTimer][TotalPluginStartup] 224.672019ms 

2014-10-23 06:56:14.432 BackTracker[1689:462317] Resetting plugins due to page load. 

2014-10-23 06:56:16.520 BackTracker[1689:462317] Finished load of: file:///private/var/mobile/Containers/Bundle/Application/532EB1D5-B92A-4939-9FBD-C1EB1E8962CD/BackTracker.app/www/index.html

2014-10-23 06:56:16.709 BackTracker[1689:462317] THREAD WARNING: ['NetworkStatus'] took '54.156250' ms. Plugin should use a background thread. 

2014-10-23 06:56:16.734 BackTracker[1689:462317] THREAD WARNING: ['StatusBar'] took '22.954102' ms. Plugin should use a background thread. 

2014-10-23 06:57:02.175 BackTracker[1689:462317] Application tried to represent an active popover presentation: <UIPopoverPresentationController: 0x17dd0900> 

2014-10-23 07:00:14.995 BackTracker[1689:462317] THREAD WARNING: ['File'] took '10.078857' ms. Plugin should use a background thread. 

2014-10-23 07:00:15.081 BackTracker[1689:462317] THREAD WARNING: ['EmailComposer'] took '56.955078' ms. Plugin should use a background thread.

【问题讨论】:

【参考方案1】:

Cordova 有多种调用本机插件的方法(本机桥接模式)。

尝试使用 XHR OPTIONAL PAYLOAD 代替默认值 IFRAME_NAV 就在 deviceready 事件之后。

exec.setJsToNativeBridgeMode(exec.jsToNativeModes.XHR_OPTIONAL_PAYLOAD);

更多信息: https://github.com/phonegap/phonegap/blob/master/lib/ios/guides/Changing%20the%20javascript%20to%20Native%20Bridge%20Mode.md

【讨论】:

我按照您的建议添加了代码,但线程警告继续出现在 xcode 控制台中。事实上,当我将代码添加到设备就绪事件中时,应用程序没有崩溃的地方就开始崩溃了。

以上是关于如何让PhoneGap IOS插件在后台线程中运行的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中使用 Admob Phonegap 插件

如何在后台应用程序时从 JavaScript 发出通知/警报 - PhoneGap iPhone/iOS

线程警告 - phonegap

iOS保持线程在后台运行

在 iOS 上单击时,Phonegap 插件推送通知保留在托盘中

iOS:如何让后台线程在主线程上重绘视图