2013-06-11 52 views
1

我正在創建一個PhoneGap該應用在應用處於活動狀態時正在跟蹤用戶的當前位置並將其顯示在地圖上。PhoneGap iOS應用無法及時恢復

當應用程序啓動時,我開始使用setIntervalnavigator.geolocation.getCurrentPosition上跟蹤位置。當我收到暫停事件時,該間隔被清除。當我收到恢復事件時,我再次撥打setInterval

而且,在接收簡歷事件,我需要發送一個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調用是異步I不會鎖定的渲染和應用程序將能夠恢復的時間,但它似乎不是那樣。我還嘗試將簡歷代碼包裝在setTimeout中,但結果相同。

什麼是更好的方法來實現這一目標,而不會無法及時恢復?

我使用PhoneGap的2.7

回答

0

你可以通過同步等待一個調用其他之前返回嘗試運行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);