我的应用程序发生了一些崩溃。检查日志并使用 atos 时,它会告诉我崩溃的确切位置,也就是我告诉我的 NSRunLoop 运行的位置:
/**
* Create a new thread for the timer
*
* @version $Revision: 0.1
*/
- (void)createTimerThread {
NSThread *timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil];
[timerThread start];
[timerThread release];
}//end
/**
* Start the actual timer
*
* @version $Revision: 0.1
*/
- (void)startTimerThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// Start timer
self.countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[runLoop run];// <--- Crash happened here
[pool release];
}//end
/**
* Update the counter
*
* @version $Revision: 0.1
*/
- (void)updateCounter:(NSTimer *)theTimer {
// Does tons of timer stuff here
}//end
如您所见,崩溃发生了,[runLoop run]
但我不知道为什么。它通常发生在我第二次调用 createTimerThread 方法时。
我在这里做错了什么?我想做的只是在后台运行一个计时器,这样它就不会在主线程上,因为我需要更新一个UILabel
.
我应该使用像 Grand Central Dispatch (GCD) 这样的新东西吗?