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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 全国各地电信DNS服务器地址:

    全国各地电信DNS服务器地址:全国各地电信DNS服务器地址:北京:202.96.199.133202.96.0.133202.106.0.20202.106.148.1202.97.16.195上海:202.96.199.132202.96.199.133202.96.209.5202.96.209.133天津:202.99.96.6810.10.64.68广东:202.96.128.143202.96.12

    2022年7月11日
    39
  • qlistwidget虚线_QlistWidget自定义item[通俗易懂]

    qlistwidget虚线_QlistWidget自定义item[通俗易懂]东西不能知道拿来用,但可以借鉴效果简单版QListWidget*list=newQListWidget;QListWidgetItem*item=newQListWidgetItem(list,0);item->setSizeHint(QSize(100,100));QWidget*w=newQWidget(list);QHBoxLayout*layout=newQHBo…

    2022年6月6日
    36
  • 怎么选择靠谱Java培训机构?[通俗易懂]

    怎么选择靠谱Java培训机构?[通俗易懂]不论是从2018年TIOBE每个月的编程语言排行榜上看,还是从各知名招聘网站年底分析的招聘报告中了解,我们都不难看出,在IT行业里Java依然是技术圈中最热门、最抢手、需求量最大的编程语言!所以,我们常常能够在知乎、百度上搜索到大量类似于“如何选择一家好的Java培训机构?选择Java培训机构应该注意哪些问题?有哪些Java培训机构值得推荐?”这些换汤不换药的提问。各类问题下的答案也是参差不…

    2022年9月1日
    3
  • Java SSM框架面试题「建议收藏」

    Java SSM框架面试题「建议收藏」Spring入门:https://blog.csdn.net/qq_40323256/article/details/89738330Spring中的动态代理和aop的自定义通知:https://blog.csdn.net/qq_40323256/article/details/89754201SpringMVC入门:https://blog.csdn.net/qq_40323256/ar…

    2022年6月16日
    32
  • 闭包面试回答_ajax面试题

    闭包面试回答_ajax面试题写在前面:在学习了闭包之后,试着做做这些题。其实是一种很棒地检验自己学习成果的手段。我当时反反复复,学了但好像又没完全学,遇到题还是一头雾水,到现在可以捋得很清楚也经历了蛮久的。而且从this,执行上下文,作用域一直走过来,这些题目涉及的内容也相对全面,加油喽。

    2022年8月30日
    3
  • 关于禅道迁移服务器

    关于禅道迁移服务器

    2021年6月4日
    153

发表回复

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

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