小编Joh*_*ohn的帖子

在线程上运行NSTimer

我正在尝试使用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)

当我删除使计时器无效的行时,一切正常.我不应该使它无效或我是否犯了其他错误?

谢谢

iphone nstimer nsthread runloop

3
推荐指数
1
解决办法
9459
查看次数

标签 统计

iphone ×1

nsthread ×1

nstimer ×1

runloop ×1