Issues with canceling and re-creating NSTimer (s)

I'm having problems starting and stopping NSTimers. Documents say the timer is stopped [timer invalidate];

I have a timer object declared as such

.h
NSTimer *incrementTimer;
@property (nonatomic, retain) NSTimer *incrementTimer;
.m
@synthesize incrementTimer;
-(void)dealloc {
 [incrementTimer release];
 [super dealloc];
 }

- Usual.

When necessary, my method does the following:

-(void)setGenCount {
    if(!condition1 && condition2) {
        incrementTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 
                                                      target: self 
                                                    selector:@selector(incrementBatteryVoltage:) 
                                                    userInfo: nil 
                                                     repeats: YES]; 
    }
}

Everything above works fine. However, once this timer does this work, I want it to invalidate itself. I will cancel the timer because there is an equal decrement method that can be called and will fight incrementTimer if it is still active. (I used to notice that my two timers, if they are active, act on the same ivar, increasing and decreasing value (a kind of battle) ... without failures). The selector works as follows:

-(void)incrementBatteryVoltage:(NSTimer *)timer {
    if(battVoltage < 24.0) {
         generatorDisplay.battVoltage += 0.1;
      }
    if(battery1Voltage == 24.0) {
         [timer invalidate];
      }
  }

, . ( )
- : . "" , , , , . , , .

-(void)deEnergizeDisplays {

   if([decrementTimer isValid]) {
        [decrementTimer invalidate];
        decrementTimer = nil;
     }

    if([incrementTimer isValid]) {
       [incrementTimer invalidate];
       incrementTimer = nil;
    }

"BAD_ACCESS" . [timer isValid]. , ... . , [timer invalidate] , . : , .

: , ? , :

timer = nil;

:

if([timer isValid])

:

if([timer != nil])

:

if(timer)

. NSTimers.

+3
2

: . . , . :

incrementTimer = [NSTimer ...

:

self.incrementTimer = [NSTimer ...

self.propertyName = ... , , , ( retain). propertyName = ... accessor. ivar .


# 2: (. ) , " " . , , :

controller.h

NSTimer *voltageTimer;
float targetBatteryVoltage;
...
@property (nonatomic, retain) NSTimer *voltageTimer;

Controller.m

@implementation Controller
@synthesize voltageTimer;

- (void)stopVoltageTimer {
    [voltageTimer invalidate];
    self.voltageTimer = nil;
}

- (void)setTargetBatteryVoltage:(float)target {
    [voltageTimer invalidate];
    targetBatteryVoltage = target;
    self.voltageTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0
                                target: self
                              selector: @selector(updateBatteryVoltage:)
                              userInfo: nil
                               repeats: YES];
}

- (void)updateBatteryVoltage:(NSTimer *)timer {
    const float increment = 0.1;
    if (abs(battVoltage - targetBatteryVoltage) < increment) {
        [timer invalidate];
    }
    else if (battVoltage < targetBatteryVoltage) {
        generatorDisplay.battVoltage += increment;
    }
    else if (battVoltage > targetBatteryVoltage) {
        generatorDisplay.battVoltage -= increment;
    }
}

, :

[self setTargetBatteryVoltage:24.0];

:

- (void)deEnergizeDisplays {
    [self stopVoltageTimer];
}
+9

retain incrementTimer setGenCount. , , self.:

self.incrementTimer = [NSTimer scheduledTimerWithTimeInterval: ...
+3

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


All Articles