NSTimer scheduledTimerWithTimeInterval与timerWithTimeInterval、initWithFireDate的区别

NSTimer scheduledTimerWithTimeInterval与timerWithTimeInterval、initWithFireDate的区别英文原文是这样的:Atimerobjectcanberegisteredinonlyonerunloopatatime,althoughitcanbeadded

大家好,又见面了,我是你们的朋友全栈君。

英文原文是这样的:

A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

 

翻译过来大体是这样的:

有三种方法来创建一个定时器

1.使用scheduledTimerWithTimeInterval

类方法创建计时器和进度上当前运行循环在默认模式(NSDefaultRunLoopMode)

2.使用timerWithTimerInterval

类方法创建计时器对象没有调度运行循环(RunLoop)

在创建它,必须手动添加计时器运行循环,通过调用adddTimer:forMode:方法相应的NSRunLoop对象

3.使用initWithFireDate

在创建它,必须手动添加计时器运行循环,通过使用addTimer:forMode:方法相应的NSRunLoop对象

 

1.

- (void)execute {
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
}

2.

- (void)execute {
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
    //为什么在主线程不需要这句run,那是因为主线程有RunLoop而且已经启动
    [[NSRunLoop currentRunLoop] run];
}

 

这两个代码效果是一样的,scheduledTimerWithTimeInterval相当于timerWithTimeInterval的两句。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154696.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql日期格式转换_MySQL日期格式转换

    mysql日期格式转换_MySQL日期格式转换DATE_FORMAT(date,format):根据参数对date进行格式化。1234SELECTDATE_FORMAT(‘2016-01-1622:23:00′,’%W%M%Y’)SELECTDATE_FORMAT(‘2016-01-1622:23:00′,’%D%y%a%d%m%b%j’)SELECTDATE_FORMAT(‘2016-01-1622:23:00’…

    2022年6月15日
    26
  • IIS7配置防盗链「建议收藏」

    IIS7配置防盗链「建议收藏」在IIS下如何实现Web的防盗链等Url重定向了,用下面的方法即可实现:1、下载微软官方提供的IISREWRITE模块:http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=1b8c7bd8-8824-4408-b8fc-49dc7f951a002、修改Web站点的web.config<syst…

    2022年7月23日
    9
  • CreateProcess和WinExec

    CreateProcess和WinExecCreateProcess非阻塞运行,而WinExec为阻塞运行,它非要等到返回时才继续执行。在两个进程共享同一个端口时,为了能让一个退出另一个申请,必须用函数CreateProcess,等到我的端口资源释放后,在运行另一个进程进行申请

    2022年7月11日
    42
  • C语言:求两个数的最大公约数和最小公倍数

    C语言:求两个数的最大公约数和最小公倍数C语言:求两个数的最大公约数和最小公倍数求两个数的最大公约数:“辗转相除法”:设两数为a和b(a>b),用a除以b,得a÷b=商…余数,若余数为0,则最大公约数为b;若余数不为0,则再用b÷余数,得b÷余数=商1…余数1,若余数1=0,则最大公约数为余数,若余数1不为0,继续让商÷余数n,一直到能够余数为零这时的除数即最大公约数。求两个数的最小公倍数:最小公倍数=两数的乘积÷…

    2022年5月13日
    45
  • python保留小数位数_python小数点保留三位

    python保留小数位数_python小数点保留三位https://www.luogu.org/problemnew/show/P14221.round()内置方法a=int(input())sum=0if(a>=401):sum+=(a-400)*0.5663a=400if(a>=151):sum+=(a-150)*0.4663a=150sum+=a*0.4…

    2022年8月11日
    7
  • CMD命令:不是内部或者外部命令也不是可运行的程序或批处理文件

    CMD命令:不是内部或者外部命令也不是可运行的程序或批处理文件前言:相信有很多小伙伴都比较喜欢使用Command命令来快速的打开或运行程序,但是有些时候命令提示符会和我们开个小玩笑。今天我就教大家如何管教这个不听话的cmd!场景:看有些大神在命令提示符里输入两句命令就能执行一大串东西,本着学习的态度,先试试再说!没成想出现了:“不是内部或外部命令,也不是可运行的程序或批处理文件。”通过各种查各种找,终于…

    2022年5月8日
    392

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号