2012-04-23 17 views
5

NSTimerを使用して指数バックオフを使用してリトライロジックを実装しようとしています。私のコードは次のようになり :NSTimerを使用して指数バックオフでリトライロジックを実装する

-(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; 
    } 

    ... 
} 

私がいる問題は、タイマーNSTimer scheduledTimerWithTimeIntervalは、私が提供し、常に0.0に設定しますよ間隔を無視しているようだということです。私がここで間違っていることに関する提案はありますか?

答えて

5

NSTimertimeIntervalプロパティに関するAppleのドキュメントがこれにあたります。

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

受信機は、非繰り返しタイマーの場合、(時間間隔が設定された場合であっても)は0を返します。

タイマーの間隔を把握するには、他の方法が必要です。あなたのクラスにiVarをお勧めします。

-(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

ありがとうございます!私は次回にドキュメントを読むべきだと思う。 :) – Ivan

関連する問題