gpt4 book ai didi

objective-c - 从NSTimer的调用方法中使其无效

转载 作者:行者123 更新时间:2023-12-01 17:27:12 26 4
gpt4 key购买 nike

我已经使用[NSTimercheduledTimerWithTimeInterval:target:selector:userInfo:]安排了一个计时器,并希望在它触发时使它无效。

- (id)init
{
[NSTimer scheduledTimerWithInterval:1 target:self selector:@selector(fired:) userInfo:nil repeats:YES];
}

- (void)fired:(NSTimer *)timer
{
if (someCondition) {
[timer invalidate];
}
}

可以吗?文档说明

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.



如果这不是完成此任务的正确方法:什么是正确方法?

最佳答案

可以通过解雇方法使它无效,因为解雇方法位于计时器计划在的同一线程上:

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.

关于objective-c - 从NSTimer的调用方法中使其无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com