iPhone 相当于用于周期性任务的 Timer 和 TimerTask

发布于 2024-12-06 19:06:10 字数 850 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫九 2024-12-13 19:06:10

您需要创建两个 NSTimer - 一个用于纪元计数器,另一个用于自动保存任务。像这样:

- (void)startTimer {

if(timerStarted){
    //avoid duplicate timers! 
}else{

    running = true;
    timerStarted = true;

    readyToProcess = true;

    epochTimer = [[NSTimer scheduledTimerWithTimeInterval:epochSeconds 
                                        target:self
                                        selector:@selector(processEpochTimer:)
                                        userInfo:nil
                                        repeats:YES] retain];

    autosaveTimer = [[NSTimer scheduledTimerWithTimeInterval:autosaveSeconds 
                                        target:self
                                        selector:@selector(processAutosaveTimer:)
                                        userInfo:nil
                                        repeats:YES] retain];
    }
}

您还需要定义以下处理程序方法,这些方法在计时器触发时调用:

- (void)processEpochTimer:(NSTimer*)theTimer;
- (void)processAutosaveTimer:(NSTimer*)theTimer;

You will need to create two NSTimers - one for the epoch counter and one for the autosave task. Something like this:

- (void)startTimer {

if(timerStarted){
    //avoid duplicate timers! 
}else{

    running = true;
    timerStarted = true;

    readyToProcess = true;

    epochTimer = [[NSTimer scheduledTimerWithTimeInterval:epochSeconds 
                                        target:self
                                        selector:@selector(processEpochTimer:)
                                        userInfo:nil
                                        repeats:YES] retain];

    autosaveTimer = [[NSTimer scheduledTimerWithTimeInterval:autosaveSeconds 
                                        target:self
                                        selector:@selector(processAutosaveTimer:)
                                        userInfo:nil
                                        repeats:YES] retain];
    }
}

You also need to define the following handler methods, which are called when the timers fire:

- (void)processEpochTimer:(NSTimer*)theTimer;
- (void)processAutosaveTimer:(NSTimer*)theTimer;
望笑 2024-12-13 19:06:10

看看 NSTimer

Take a look at NSTimer

小草泠泠 2024-12-13 19:06:10

看一下 http://www.iphoneexamples.com/ 计时器

计时器
该计时器将每 1 秒调用一次 myMethod。

[NSTimer scheduledTimerWithTimeInterval:1 
    target:self 
    selector:@selector(myMethod:) 
    userInfo:nil 
    repeats:YES];

如果您需要将对象传递给 myMethod 怎么办?使用“userInfo”属性。
1. 首先创建 Timer

[NSTimer scheduledTimerWithTimeInterval:1 
    target:self 
    selector:@selector(myMethod:) //don't forget the ":"
    userInfo:myObject 
    repeats:YES];
  1. 然后将 NSTimer 对象传递给您的方法:

    -(void) myMethod:(NSTimer*)计时器
    // 现在我可以访问 myObject 的所有属性和方法
    [[timer userInfo] myObjectMethod];

要停止计时器,请使用“invalidate”:

[myTimer invalidate];
myTimer = nil; // ensures we never invalidate an already invalid Timer

take a look at http://www.iphoneexamples.com/ Timer

Timers
This timer will call myMethod every 1 second.

[NSTimer scheduledTimerWithTimeInterval:1 
    target:self 
    selector:@selector(myMethod:) 
    userInfo:nil 
    repeats:YES];

What if you need to pass an object to myMethod? Use the "userInfo" property.
1. First create the Timer

[NSTimer scheduledTimerWithTimeInterval:1 
    target:self 
    selector:@selector(myMethod:) //don't forget the ":"
    userInfo:myObject 
    repeats:YES];
  1. 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":

[myTimer invalidate];
myTimer = nil; // ensures we never invalidate an already invalid Timer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文