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)에서 실행되고 있음을 알 수 있습니다.
답장을 보내 주셔서 감사합니다. 동일한 논리를 구현할 수 있도록 일부 코드 블록을 제안하십시오. –