Running NSTimer on a thread

谁说我不能喝 提交于 2019-12-06 00:46:12

Try

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

instead. You will have to make timerThread an ivar of your view controller.

N. Anant Vijay
    - (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];
    }
luck

I guess you have done some UI work in "method".

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

From the log, it seems you have done UI updating work it in "Timer" thread in "method".

you can use block to dispatch work on main thread or performSeletorOnMainThread for doing "method"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!