NStimer APIhttp://hi.baidu.com/douxinchun/item/920a7655fe7f3fd8d48bacaf

来源:互联网 发布:同程网络 编辑:程序博客网 时间:2024/04/25 17:17

新增:关于nstimer 内存问题

网址:http://hi.baidu.com/douxinchun/item/920a7655fe7f3fd8d48bacaf

-(void)viewDidDisappear:(BOOL)animated

{

     [self.orderTimerinvalidate];

}

在使用的过程中,在上述方法中停止定时器,dealloc方法才会及时调用,原因未明;


[timer release] only needs to be called if you “own” the timer. From Apple’s documentation:

Because the run loop maintains the timer, from the perspective of memory management there’s typically no need to keep a reference to a timer once you’ve scheduled it. Since the timer is passed as an argument when you specify its method as a selector, you can invalidate a repeating timer when appropriate within that method. In many situations, however, you also want the option of invalidating the timer—perhaps even before it starts. In this case, you do need to keep a reference to the timer, so that you can send it an invalidate message whenever is appropriate. If you create an unscheduled timer (see “Unscheduled Timers”), then you must maintain a strong reference to the timer (in a reference-counted environment, you retain it) so that it is not deallocated before you use it.

What does this mean?

If you alloc and init a timer, you must also release it, like so:

NSTimer * timer = [[NSTimer alloc] initWith…];

NSRunLoop * runLoop = [NSRunLoop currentRunLoop];

 

[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

 

[timer release];

……

[timer invalidate];

 

timer = nil;

 

Once the timer has been added to the run loop, there is no reason to keep a reference to it anymore, since the run loops owns it. In this case, as shown, you would release the timer as soon as you add it to the run loop, and then simplyinvalidate it when you are finished. The final line (setting timer to nil) is for safety. The call to invalidate will result in the timer being released (by the run loop), so it is unsafe to keep a reference that points to it. Setting the local reference to nil keeps things kosher.

If, however, you create a timer using one of the convenience methods like so:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval ...];

 

You do not need to call [timer release] at all!

 

The convenience method adds the timer to the run loop, which then owns it, so you do not need to perform any memory management on the returned timer object. You would simplyinvalidate the timer when you no longer want to use it:

[timer invalidate];timer = nil;

 

Or, if the timer was not set to repeat, you would do absolutely nothing at all, since it would be released after its first invocation.


译文:[timer release]这个方法只有在当你拥有timer的时候才可以调用。根据Apple的文档描述如下:由于RunLoop保持着timer。从内存管理的角度上看,当我们scheduled(预定)一个timer的时候,我们通常是不需要保持它的reference(引用计数)的。由于timer是作为一个参数传递的,timer的指定的方法是selector的形式,所以你可以在指定的方法的内部,在合适的时候invalidate一个重复的timer(repeating timer)。然而,在许多的场合下,你可能会想在timer开始之前就invalidating timer。在这种情况下,又必须保持一个对timer的引用(reference),以便于你可以合适的时间向timer发送invalidate消息。如果你创建了一个 unscheduled timer,你必须保持一个对于这个timer的 strong reference(在 reference_count(引用计数的环境下),你给timer发送了一个retain的消息),n这样做目的是保证在你使用该timer之前,它不会被deallocated。简言之:如果你allloc init了一个timer,你必须release it,例如:

NSTimer * timer = [[NSTimer alloc] initWith…];

NSRunLoop * runLoop = [NSRunLoop currentRunLoop];

[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

[timer release];

……

[timer invalidate];

timer = nil;

一旦timer被加入了Runloop,我们就没有任何原因来保持一个引用,因为Runloop会保持它。在这个展示中,正如我们我展示的,你应该release 掉它,当你把它添加进Runloop的时候,并且像示例中的那样在timer结束的时候 invalidate 。最后一行代码(timer = nil)是为了安全。invalidate 的调用会使得timer被release(由Runloop控制),所以保持一个指向timer的引用是不安全。将本地的引用设置为空是符合规则的。

无论如何,如果你创建一个timer按照下面的这种形式:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval ...];

你根本不需要 调用 [timer release]!

这个便捷的方法,把timer加入到了Runloop,Runloop会保持timer的引用计数,所以你不需要在返回的timer对象上实现任何的

内存管理。你应该想示例的那样invalidate timer,如下:

[timer invalidate];timer = nil;

或者,如果timer没有设置重复(repeat),你就安心的不需要做任何的事情,因为他会在第一次调用完成后release timer。



转载地址:http://blog.csdn.net/davidsph/article/details/7899731

初始化方法

+ scheduledTimerWithTimeInterval:invocation:repeats:
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

这两个是创建一个定时器,并加入到当前运行循环中,即我们可以这样去理解,这样初始化一个定时器时,在(NSTimeInterval)seconds 时间之后,自动启动定时器。


而以下两个初始化方法这不一样:

+ timerWithTimeInterval:invocation:repeats:
+ timerWithTimeInterval:target:selector:userInfo:repeats:


这两个同样是创建,但没有加入到,运行循环中。class method to create the timer object without scheduling it on a run loop.然后,建立之后,必须(after creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.),这就是与上面两个方法的区别。

  //创建一个定时器
       _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];


_timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
       //必须手动加入到当前循环中去
       NSRunLoop *runloop=[NSRunLoop currentRunLoop];
       [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];

以上两段代码效果是一样的


- (void)fire;

其实他并不是真的启动一个定时器,从之前的初始化方法中我们也可以看到,建立的时候,在适当的时间,定时器就会自动启动。那这个方法是干什么的呢。

You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

这是官方文档的说法,英文说的很清楚,但我们理解还不是很到位,为了彻底搞懂它的功能。我又做了一个测试。


也是很简单,把上面那个定时器,改变一点

//初始化的时候创建一个定时器

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //创建一个定时器,
       _timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

//手动加入到循环中

      NSRunLoop *runloop=[NSRunLoop currentRunLoop];

 [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];

//当然这个定时器会自动启动,只不多过了十秒之后,才触发

}

return self

}


当我们单击“开始”按钮时,

- (IBAction)startTime:(id)sender {
   
    //只是简单地调用一下这个方法,看到底功能是什么
  [_timer fire];

}


结果是,单击一下按钮,倒计时减1,单击一下减1,即它把触发的时间给提前了,但过十秒后倒计时还会减1,即它只是提前触发定时器,而不影响之前的那个定时器设置的时间,就好比我们等不及要去看一场球赛,赶紧把车开快些一样,fire的功能就像让我们快些到球场,但却不影响球赛开始的时间。

还记得之前那个初始化定时器时,设置的是YES吗,当我们,改为NO时,即不让它循环触发时,我们此时再单击开始按钮。会猛然发现,倒计时减1了,但当我们再点击开始按钮时,会发现倒计时,不会动了。原因是:我们的定时器,被设置成只触发一次,再fire的时候,触发一次,该定时器,就被自动销毁了,以后再fire也不会触发了。

现在 我们再看官方的解释,或许就会更明白了,

You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

这就对了,fire并不是启动一个定时器,只是提前触发而已。


0 0
原创粉丝点击