3

iPhoneSDK3.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];
}

タイマーを無効にする行を削除すると、すべてが正常に機能します。私はそれを無効にすることになっていないのですか、それとも他の間違いを犯していますか?

ありがとう

4

3 に答える 3

7

試す

[timer performSelector:@selector(invalidate) onThread:timerThread withObject:nil waitUntilDone:NO];

代わりは。timerThread をビュー コントローラーのivarにする必要があります。

于 2010-02-04T16:57:03.957 に答える
2
    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(timerStart) userInfo:nil repeats:TRUE];
    [timerThread start]; 
    }

    -(void)timerStart
    {
   @autoreleasePool{
    NSRunLoop *TimerRunLoop = [NSRunLoop currentRunLoop];
    [NSTimer scheduleTimerWithInterval:0.1 target:self selector:@selector(methodName:) userInfo:nil repeat:YES];
    [TimerRunLoop run];
    }

    - (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    }
于 2012-10-19T11:42:22.620 に答える
0

" " でいくつかの UI 作業を行ったと思いますmethod

 timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(method) userInfo:nil repeats:YES] retain];

ログを見ると、「メソッド」の「タイマー」スレッドでUI更新作業を行っているようです。

ブロックを使用して、メインスレッドで作業をディスパッチしたりperformSeletorOnMainThread、「メソッド」を実行したりできます

于 2012-06-19T06:03:57.127 に答える