我用的NSTimer一起使用NSThread。的NSTimer scheduledTimerWithTimeInterval:重复:NO选择方法调用多次
我的代码是这样的
-(void) checkForRecentAlarm
{
if ([self.alarmThread isFinished])
{
[self.alarmThread cancel];
}
self.alarmThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerForRecentAlarm) object:nil];
[self.alarmThread start];
//[NSThread detachNewThreadSelector:@selector(startTimerForRecentAlarm) toTarget:self withObject:nil];
}
-(void)startTimerForRecentAlarm
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
self.recentAlarmTime = [NSDate date];
self.dbObject = [[RADataBaseModelManager alloc] init];
self.recentAlarmTime = [self.dbObject getMostRecentAlarmTimeFromDB];
if (self.recentAlarmTime) {
NSTimeInterval timeIntervalToAlarm = [self.recentAlarmTime timeIntervalSinceNow];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
//Fire timer every second to updated countdown and date/time
self.RATimer = [NSTimer scheduledTimerWithTimeInterval:timeIntervalToAlarm target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
[runLoop run];
}
[pool release];
}
- (void)timerFireMethod:(NSTimer*)theTimer
{
[self.RATimer invalidate];
[theTimer invalidate];
self.RATimer = NULL;
theTimer = NULL;
[self playAlarm];
UIAlertView *alarmAlert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"Snooze", nil];
[alarmAlert show];
[alarmAlert release];
alarmAlert = nil;
}
现在的问题是,我alertbox来两次在 startTimerForRecentAlarm 方法一个呼叫。所以警报来了两次,我的看法卡住了。
这里会出现什么问题?
我想实现使用单一的NSTimer有多个报警选项报警。
请帮助。
当调试这一点,我可以找到很多并发线程对同一代码(UIAlertView中)上运行。
谢谢您的回复。请给我一些代码块,以便我可以实现相同的逻辑。 –