Joh*_*ohn 3 iphone nstimer nsthread runloop
我正在尝试使用iPhone SDK 3.0在线程上运行NSTimer.我想我正在做的一切正确(新的runloop等).如果我在viewDidDissappear上调用[timer invalidate]虽然我收到此错误:
bool _WebTryThreadLock(bool),0x3986d60:试图从主线程或Web线程以外的线程获取Web锁.这可能是从辅助线程调用UIKit的结果.现在崩溃...程序收到信号:"EXC_BAD_ACCESS".
这是我的代码:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[activityIndicator startAnimating];
NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(timerStart) object:nil]; //Create a new thread
[timerThread start]; //start the thread
}
-(void)timerStart
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
//Fire timer every second to updated countdown and date/time
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(method) userInfo:nil repeats:YES] retain];
[runLoop run];
[pool release];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[timer invalidate];
}
Run Code Online (Sandbox Code Playgroud)
当我删除使计时器无效的行时,一切正常.我不应该使它无效或我是否犯了其他错误?
谢谢
尝试
[timer performSelector:@selector(invalidate) onThread:timerThread withObject:nil waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)
代替.您必须使timerThread成为视图控制器的ivar.
| 归档时间: |
|
| 查看次数: |
9459 次 |
| 最近记录: |