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

相关推荐

  • 牛客网–质因数的个数

    牛客网–质因数的个数

    2021年5月18日
  • VUE响应式原理-Dep类「建议收藏」

    VUE响应式原理-Dep类「建议收藏」classDep{constructor(){//存储订阅者this.subs=[]}//添加订阅者addSub(sub){if(sub&&sub.update){this.subs.push(sub)}}//通知订阅者notify(){//变量订阅者,执行更新this.subs.forEach(item=>item.update()).

    2022年5月26日
  • CICD简介[通俗易懂]

    CICD简介[通俗易懂]CI/CD​CI/CD的出现改变了开发和测试人员发布软件的方式。​传统的软件开发和交付方式在迅速变得过时。过去的敏捷时代里,大多数公司的软件发布周期是每月、每季度甚至每年,而在现在DevOps时代,每周、每天甚至每天多次都是常态。当SaaS(软件即服务)成为业界主流后尤其如此,您可以轻松地动态更新应用程序,而无需强迫用户下载更新组件。很多时候,用户甚至都不会注意到正在发生变化。开发团队通过软件交付流水线(Pipeline)实现自动化,以缩短交付周期,大多数团队都有自动化流程

    2022年6月10日
  • WinRAR去除广告,亲测可用

    WinRAR去除广告,亲测可用原文地址:最新去除中文winrar压缩软件弹出广告方法-百度经验(baidu.com) 此文用作备用文,在原文看不到的情况下,参考本文。感谢原文作者!!! 安装RAR简体中文版 下载并安装Restorator2007 找到RAR安装文件夹,可以双击打开RAR.exe看是否弹出广告 打开Restorator2007 选着左上角“文件-打开”,在“C:\ProgramFiles\WinRAR”文件夹中打开“WinRAR.exe” 打开后.

    2022年6月3日
  • Mysqldump备份报错1449

    Mysqldump备份报错1449Mysqldump备份报错1449

    2022年4月24日
  • 分辨率_分辨率越高越好?手机屏幕分辨率多少才合适?现在终于搞清楚了[通俗易懂]

    分辨率_分辨率越高越好?手机屏幕分辨率多少才合适?现在终于搞清楚了[通俗易懂]最近出了很多新机,很多人在购买前会详细查看手机参数,其中“分辨率”这一项让不少人一头雾水,究竟手机分辨率是什么?对我们的使用体验有什么影响?是不是分辨率越高越好?​教授估计这些问题让很多对手机了解不多的朋友产生困扰,所以今天就来为大家科普科普~一、什么是手机屏幕分辨率?图像的显示都是由许多像素点排列组成的,手机屏幕分辨率就代表着像素个数,当用手机屏幕中横向的像素点与竖向的像素点相乘的时候,再换算出…

    2022年4月19日

发表回复

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

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