How to work with NSTimer

I use a lot of timers in my application. For recording time, moving an object, attenuation, etc. I use the same timer for several dolls in the same view at different times. How can I declare and revoke or release my timers properly?

Atm Im announces timers as follows:

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

and as soon as im won't use it, follow these steps:

[fadeTimer invalidate]; 
fadeTimer = nil;

The hold counter when im leaving the view is 0 on each timer. Should I release a timer in dealloc? My application works well, but it crashes from time to time.

ClockTimer, which I use to update tags using time, uses

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

Do I need to do anything with this mainLoop after I do invalidate clockTimer?

, , .

!

+3
3

- , . ,

@property (nonatomic, retain) NSTimer *fadeTimer;

,

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;

, . , iPhone. , , , ;)

, , , NSTimer - ?

+7
  • . , , ( -, , ).
  • , , ( ). "" . clockTimer, fadeTimer, .
  • " " . , . , .
+3
*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;
    }*
-1

Source: https://habr.com/ru/post/1758725/


All Articles