您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页如何在后台跑NSTimer,永不停歇

如何在后台跑NSTimer,永不停歇

来源:二三四教育网

- (void)viewDidLoad {

             [super viewDidLoad];

           //保证timer在后台运行

           [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];

         //创建并执行新的线程

          NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread) object:nil];

}

 //开一个新线程

- (void)newThread

    {

               @autoreleasepool

    {

//在当前Run Loop中添加timer,模式是默认的NSDefaultRunLoopMode

               [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timer_callback) userInfo:nil repeats:YES];

               //开始执行新线程的Run Loop

            [[NSRunLoop currentRunLoop] run];

     }

}

//定时器

- (void)timer_callback

{

                NSLog(@"根本停不下来");

}

下面上图

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务