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


相关推荐

  • 下载MODIS数据「建议收藏」

    下载MODIS数据「建议收藏」下载Modis数据

    2022年5月26日
    34
  • SpringBoot的认识,SpringBoot与Spring关系[通俗易懂]

    SpringBoot的认识,SpringBoot与Spring关系[通俗易懂]一、概念1、SpringSpring是一个开源容器框架,可以接管web层,业务层,dao层,持久层的组件,并且可以配置各种bean,和维护bean与bean之间的关系。其核心就是控制反转(IOC),和面向切面(AOP),简单的说就是一个分层的轻量级开源框架。2、SpringMVCSpringMVC属于SpringFrameWork的后续产品,已经融合在SpringWebFlow里面。SpringMVC是一种web层mvc框架,用于替代servlet(处理|响应请求,获取表单参数,表单校验等。S

    2022年5月27日
    35
  • 嵌套对象转map

    嵌套对象转map嵌套对象转map,当对象嵌套层次太深,获取子对象的值及其不便,为解决这一问题,于是对象转mpa,有key就能得到相应的value。解决复杂json情况,尤其是当第三方json过于复杂时候很适合,如央行征信报告等。java代码://测试json,可以为一个Object对像Stringjson=”{\”success\”:0,\”errorMsg\”:\”错误消息\”,\…

    2022年5月17日
    38
  • 二叉树前序遍历详解[通俗易懂]

    二叉树前序遍历详解[通俗易懂]二叉树的遍历是数据结构中非常基础的内容了,今天这一篇文章我们来详细了解一下二叉树的前序遍历,二叉树的前序遍历顺序是根节点-左子树-右子树,本文对递归和栈模拟的方法都有实现一、递归方法递归方法可以说是很简了,我们秉承先去往左节点再去往右节点的原则就好了//assumethatwehaveTreeNode,andresistostoretheanswervoidpreorder(TreeNode*root,vector<int&.

    2025年10月20日
    2
  • DLL注入explorer.exe进程[通俗易懂]

    DLL注入explorer.exe进程[通俗易懂]**DLL注入explorer.exe进程**  最近一直在学习dll注入远程进程的相关知识,于是有了这篇文章。通过注入的方式会运行程序,在资源管理器中是看不到,相关的进程的,这为程序的隐藏提供了极大的便利。一、新建dll动态链接库,然后在dllmain.cpp文件中的“caseDLL_PROCESS_ATTACH:”下输入当你dll被进程加载时要执行的代码。这里我们用“Messag…

    2022年5月17日
    50

发表回复

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

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