iPhone 相当于用于周期性任务的 Timer 和 TimerTask
我正在尝试将 Android 应用程序移植到 iPhone。在 Android 上,我可以使用带有 TimerTasks 的 Timer 类(使用 ScheduleAtFixedRate)轻松地每 60 秒处理一次数据: timer.scheduleAtFixedRate(task,15000, epochLengthMs);
谢谢!
有类似的东西可以在 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);
}
}
I'm trying to port an Android app to an iPhone. On Android I could easily process data every 60 seconds by using a Timer class with TimerTasks using scheduleAtFixedRate:
timer.scheduleAtFixedRate(task,15000, epochLengthMs);
Thank you!
Is there something similar I can use on 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要创建两个 NSTimer - 一个用于纪元计数器,另一个用于自动保存任务。像这样:
您还需要定义以下处理程序方法,这些方法在计时器触发时调用:
You will need to create two NSTimers - one for the epoch counter and one for the autosave task. Something like this:
You also need to define the following handler methods, which are called when the timers fire:
看看 NSTimer
Take a look at NSTimer
看一下 http://www.iphoneexamples.com/ 计时器
计时器
该计时器将每 1 秒调用一次 myMethod。
如果您需要将对象传递给 myMethod 怎么办?使用“userInfo”属性。
1. 首先创建 Timer
然后将 NSTimer 对象传递给您的方法:
-(void) myMethod:(NSTimer*)计时器
// 现在我可以访问 myObject 的所有属性和方法
[[timer userInfo] myObjectMethod];
要停止计时器,请使用“invalidate”:
take a look at http://www.iphoneexamples.com/ Timer
Timers
This timer will call myMethod every 1 second.
What if you need to pass an object to myMethod? Use the "userInfo" property.
1. First create the Timer
Then pass the NSTimer object to your method:
-(void) myMethod:(NSTimer*)timer
// Now I can access all the properties and methods of myObject
[[timer userInfo] myObjectMethod];
To stop a timer, use "invalidate":