我試圖將Android應用程序移植到iPhone。在Android上,我可以使用TimerAtFixedRate通過使用TimerTasks每60秒輕鬆處理數據: timer.scheduleAtFixedRate(task,15000,epochLengthMs);針對定期任務的iPhone等價物Timer和TimerTask
謝謝!
我可以在iPhone上使用類似的東西嗎?
protected void startTimer(){
if(timerStarted){
//avoid duplicate timers!
}else{
running = true;
timerStarted = true;
if(D)Log.w(TAG,"*Timer Started*");
timer = new Timer();
readyToProcess = true;
EpochCounterTask task = new EpochCounterTask();
AutoSaveTask saveTask = new AutoSaveTask();
//give statMagnitude enough time to get values
//after 15 sec, every 60 sec
timer.scheduleAtFixedRate(task,15000, epochLengthMs);
timer.scheduleAtFixedRate(saveTask,645000, 600000);
}
}
這個偉大的工程。定時器每隔幾秒就會觸發一次。感謝最後的保留,我忘記了這一點,這可能會帶來一些不好的後果! –
定時器在被添加到runloop時會被保留,所以它們仍然是'live'。這裏的「保留」僅用於嚴格遵守Objective C內存管理約定。我假設epochTimer和autosaveTimer是你的類中的數據成員,並且你需要保留它們,這樣你可以在以後停止定時器(調用invalidate然後發佈)。 –