美文网首页
定时器在子线程中的使用

定时器在子线程中的使用

作者: FengxinLi | 来源:发表于2015-12-23 16:22 被阅读1044次

    最近需要用多线程比较多,由于我们程序中连接数据,然后更新界面。由于有可能连接断开,如果在主线程中运行的话,会卡死界面。所以用一个子线程来处理数据连接。但是我在子线程中启用定时器发现根本没有调用。结果是在子线程中不一样。

    一般在主线程中我们都是一行代码搞定。

    _timer = [NSTimer scheduledTimerWithTimeInterval:temp/100 target:self selector:NSSelectorFromString(@"testTimerOut:") userInfo:nil repeats:YES];

    但是在子线程中不行 需要加上NSRunLoop

    _timer = [NSTimer scheduledTimerWithTimeInterval:temp/100 target:self selector:@selector(testTimerOut:) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];

    [[NSRunLoop currentRunLoop] run];

    真的是要多用多写才能让自己懂得更多

    相关文章

      网友评论

          本文标题:定时器在子线程中的使用

          本文链接:https://www.haomeiwen.com/subject/acdvhttx.html