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)
上一篇 2022年7月1日 下午1:00
下一篇 2022年7月1日 下午1:00


相关推荐

  • SDK和API的区别

    SDK和API的区别不知道区别的人 大概率是你还没搞懂 API SDK 是什么 讲个小故事 研发人员 A 开发了软件 A 研发人员 B 正在研发软件 B 有一天 研发人员 B 想要调用软件 A 的部分功能来用 但是他又不想从头看一遍软件 A 的源码和功能实现过程 怎么办呢 研发人员 A 想了一个好主意 我把软件 A 里你需要的功能打包好 写成一个函数 你按照我说的流程 把这个函数放在软件 B 里 就能直接用我的功能了 其中 API 就是研发人员 A 说的那个函数 这就是 API 的诞生 日常生活中 我们有很多类似 API 的场景 比如 电脑需要调用手机里面的信息 这时

    2026年3月20日
    2
  • QT布局:QVBoxLayout/QHBoxLayout[通俗易懂]

    QT布局:QVBoxLayout/QHBoxLayout[通俗易懂]关系图垂直布局:QVBoxLayout 先来一两代码:QWidget*widget=newQWidget();QVBoxLayout*vBoxLayout=newQVBoxLayout();QLabel*label=newQLabel(“hello”);QPushButton*pushButton=newQPushButton(“hi”);vBoxLayout->addWidget(label);vBoxLayout->addWid…

    2022年6月21日
    40
  • vue中使用Ueditor编辑器

    vue中使用Ueditor编辑器

    2021年10月11日
    68
  • 硬核,字节一口气开源了两个类Manus智能体项目

    硬核,字节一口气开源了两个类Manus智能体项目

    2026年3月15日
    1
  • mssql datetime smalldatetime 字段类型插入代码「建议收藏」

    mssql datetime smalldatetime 字段类型插入代码「建议收藏」
    如果mssql字段类型是datetime ,插入代码如下
                   ps=conn.prepareStatement(strSQL2);
                   ps.setString(1,(newjava.util.Date()).toLocaleString());  
                   ps.executeUpdate();
     
    如果mssql字段类型

    2022年5月12日
    33
  • linux线程同步有几种方法_shell多线程执行命令

    linux线程同步有几种方法_shell多线程执行命令Linux中的线程同步机制(一)–Futex引子在编译2.6内核的时候,你会在编译选项中看到[*]Enablefutexsupport这一项,上网查,有的资料会告诉你”不选这个内核不一定能正确的运行使用glibc的程序”,那futex是什么?和glibc又有什么关系呢?1.什么是FutexFutex是FastUserspacemuTexes的缩写

    2025年12月11日
    5

发表回复

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

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