【问题标题】:PhoneGap iOS app fails to resume in timePhoneGap iOS 应用无法及时恢复
【发布时间】:2013-06-11 15:35:54
【问题描述】:

我正在创建一个 PhoneGap 应用,当应用处于活动状态时,它会跟踪用户的当前位置并将其显示在地图上。

当应用程序启动时,我开始使用setInterval 上的navigator.geolocation.getCurrentPosition 跟踪位置。当我收到 pause 事件时,该间隔被清除。当我收到 resume 事件时,我再次调用setInterval

此外,在接收到 resume 事件时,我需要向服务器发送一个 Ajax 请求,以查看应用程序在后台时是否发生了任何有趣的事情,并且可能会显示一个新屏幕信息。

有时,几乎从不在我的手机上,但更常见的是在测试人员的手机上,简历花费了太多时间并且应用程序被 iOS 杀死:

Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2013-05-29 09:26:54.352 +0200
OS Version:      iOS 6.1.4 (10B350)
Report Version:  104

Exception Type:  00000020
Exception Codes: 0x000000008badf00d
Highlighted Thread:  0

Application Specific Information:
com.x failed to resume in time

Elapsed total CPU time (seconds): 10.590 (user 10.590, system 0.000), 53% CPU 
Elapsed application CPU time (seconds): 9.821, 49% CPU

以下伪代码显示了我如何连接应用程序:

(function app() {

    function init() {
        // Init app
        // ...

        startGPS();

        checkState();
    }

    function checkState() {
        // Make Ajax request to server and compare with local state
        // and decide which screen to show
        // ...
    }

    function startGPS() {
        // use setInterval on navigator.geolocation.getCurrentPosition
        // ...
    }

    function stopGPS() {
        // clear the interval from startGPS()
        // ...
    }

    // Other functions
    // ...

    on('pause', stopGPS);
    on('resume', startGPS)
    on('resume', checkState);
    on('deviceready', init);

})();

我,也许是天真地认为,由于 Ajax 调用和 getCurrentPosition 调用都是异步的,我不会锁定渲染并且应用程序能够及时恢复,但它似乎不是那样。我还尝试将我的 resume 代码包装在 setTimeout 中,但结果相同。

在不及时恢复的情况下实现这一目标的更好方法是什么?

我正在使用 PhoneGap 2.7

【问题讨论】:

    标签: ios cordova


    【解决方案1】:

    您可以尝试同步运行 startGPS() 和 checkState() 函数,方法是等待其中一个函数返回,然后再调用另一个函数。尝试两种方式,因为这两个功能之一可能正在敲击 CPU 导致您的应用程序被杀死:

    function startGPS() {
      // use setInterval on navigator.geolocation.getCurrentPosition
    }
    function checkState(){
      $.ajax({
        url: someurl,
        success: function(){
          // Make Ajax request to server and compare with local state
          startGPS();
        }
      });
    }
    on('resume', checkState);
    

    function startGPS() {
      var firstCall = true;
      setInterval(function(){
        navigator.geolocation.getCurrentPosition(function(position){
          // do some stuff with position...
    
          if(firstCall){
            firstCall = false;
            checkState(); // check state after first successful position is returned
          }
        });
      }, someInterval);
    }
    function checkState(){
      // Make Ajax request to server and compare with local state
    }
    on('resume', startGPS);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多