2015-10-14 41 views
1

我有一個使我的計時器無效的問題。NSTimer不會使無效

@property (nonatomic, strong) NSTimer *timer;

裏面我對成功的塊,我分配和設置我的計時器在主線程:

dispatch_async(dispatch_get_main_queue(), ^{ 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:[Config refreshInterval].integerValue target:self selector:@selector(methodThatHasABlockCalledMentionedAbove) userInfo:nil repeats:NO]; 
}); 

在我textFieldDidBeginEditing我無效我的計時器是這樣的:

dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.timer invalidate]; 
     self.timer = nil; 
}); 

我的方法仍然在定時器上執行。有什麼收穫?

+0

檢查,如果當你調用調度異步self.timer不是零... 'assert(self.timer)' –

+0

我查過了。這不是零 – Flipper

+0

你是否可以設置幾個計時器對象而不會使舊的無效? –

回答

3

也許你正在設置幾個計時器對象,而不是先使舊的無效??

釋放Timer對象已計劃不會因此而失效計時器多數民衆贊成,所以:

你做self.timer = ....之前,請撥打[self.timer invalidate];