开发者

How to work with NSTimer

开发者 https://www.devze.com 2023-01-10 15:36 出处:网络
Im using alot of timers in my application. For recording time, moving object, fading etc. I use the same timer for several puposes in the same view at different times. How should I declare and invalid

Im using alot of timers in my application. For recording time, moving object, fading etc. I use the same timer for several puposes in the same view at different times. How should I declare and invalidate or release my timers properly?

Atm Im declaring the timers like this:

fadeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(bortInfo) userInfo:nil repeats:YES];

and as soon as im not using it im doing this:

[fadeTimer invalidate]; 
fadeTimer = nil;开发者_JAVA技巧

The retain count when im leaving the view is 0 on every timer. Should i release the timer in the dealloc aswell? My app runs quite good, but from time to time it crashes.

The clockTimer that i use for updating a label with the time uses

[[NSRunLoop mainRunLoop] addTimer:clockTimer forMode:NSRunLoopCommonModes];

Do i need to do anything with this mainLoop once i invalidate the clockTimer?

All in all please support me with some info about working with timers.

Thank you very much!

Joakim


You're not retaining your timers properly - if you want to refer to them again you should retain them. I'd do this with a property i.e. in your header file

@property (nonatomic, retain) NSTimer *fadeTimer;

and change your code to say

self.fadeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(bortInfo) userInfo:nil repeats:YES];

// Put this whenever you want to remove your timer and in your dealloc method.
[fadeTimer invalidate];
self.fadeTimer = nil;

This will make sure that your timer is retained by your object. Otherwise you just have to hope that the timer stays around and doesn't get autoreleased by the iPhone. And as you say it's crashing occasionally, this might be the reason ;)

I'm afraid I don't know much about run loop but am confused why your don't just use a normal NSTimer to schedule things - why bother interacting with the run loop at all?


  • Scheduled timers are retained by the run loop, and retain their target. If you want to retain the timer, you have to jump through a few hoops to prevent a retain cycle (I wrote a non-retaining proxy class, which is a bit messy but it works).
  • Don't manipulate the run loop unless you know what you're doing (I don't). A "scheduled" timer is already added to the main run loop. If you're generating clockTimer like fadeTimer, then it's being added to the run loop twice.
  • "from time to time it crashes" doesn't help anyone. Run it in the debugger and see where it crashes. It might even print some messages to the console if you're lucky.


*also you can use and this is a better and optimize way to write this line
if (theTimer != nil) {
        if([theTimer isValid]){
            [theTimer invalidate];
        }
        theTimer = nil;
    }*
0

精彩评论

暂无评论...
验证码 换一张
取 消