2012-04-23 15 views
5

Próbuję zaimplementować logikę ponowną z wykładniczym wycofaniem przy użyciu NSTimer. Mój kod wygląda następująco:Używanie NSTimer do implementacji logiki ponawiania z wykładniczym przesunięciem

-(void)start 
{ 
    [NSTimer scheduledTimerWithTimeInterval:0.0 target:self 
    selector:@selector(startWithTimer:) userInfo:nil repeats:NO]; 
} 

-(void)startWithTimer:(NSTimer *)timer 
{ 
    if (!data.ready) { 
    // timer.timeInterval == 0.0 ALWAYS! 
    NSTimeInterval newInterval = timer.timeInterval >= 0.1 ? timer.timeInterval * 2 : 0.1; 
    newInterval = MIN(60.0, newInterval); 
    NSLog(@"Data provider not ready. Will try again in %f seconds.", newInterval); 
    NSTimer * startTimer = [NSTimer scheduledTimerWithTimeInterval:newInterval target:self 
     selector:@selector(startWithTimer:) userInfo:nil repeats:NO]; 
    // startTimer.timeInteval == 0.0 ALWAYS! 
    return; 
    } 

    ... 
} 

Problem mam jest to, że zegar NSTimer scheduledTimerWithTimeInterval zdaje się ignorować interwał mam dostarczaniu i zawsze ustawia go na 0.0. Jakieś sugestie dotyczące tego, co robię źle tutaj?

Odpowiedz

5

Dokumentacja Apple ma do powiedzenia na temat właściwości timeInterval na NSTimer.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html

Jeśli odbiornik jest nie powtarzające czasowy powraca 0 (nawet, jeżeli przedział czasu, została ustawiona).

Będziesz musiał użyć innych środków, aby śledzić, jaki powinien być przedział czasowy. Polecam iVar na twojej klasie.

-(void)start 
{ 
    _timeInterval = 0.0; 
    [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self 
    selector:@selector(startWithTimer:) userInfo:nil repeats:NO]; 
} 

-(void)startWithTimer:(NSTimer *)timer 
{ 
    if (!data.ready) { 
    _timeInterval = _timeInterval >= 0.1 ? _timeInterval * 2 : 0.1; 
    _timeInterval = MIN(60.0, _timeInterval); 
    NSLog(@"Data provider not ready. Will try again in %f seconds.", _timeInterval); 
    NSTimer * startTimer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self 
     selector:@selector(startWithTimer:) userInfo:nil repeats:NO]; 
    return; 
    } 

    ... 
} 
+0

Dzięki! Chyba powinienem przeczytać dokumenty następnym razem. :) – Ivan

Powiązane problemy