Scheduled NSTimer后台运行
来源:互联网 发布:台军反攻知乎 编辑:程序博客网 时间:2024/06/02 01:48
- (void)viewDidLoad{ // Avoid a retain cycle __weak ViewController * weakSelf = self; // Declare the start of a background task // If you do not do this then the mainRunLoop will stop // fireing when the application enters the background self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; // Make sure you end the background task when you no longer need background execution: // [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Since we are not on the main run loop this will NOT work: [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerDidFire:) userInfo:nil repeats:YES]; // This is bacause the |scheduledTimerWithTimeInterval| uses // [NSRunLoop currentRunLoop] which will return a new background run loop // which will not be currently running. // Instead do this: NSTimer * timer = [NSTimer timerWithTimeInterval:0.5 target:weakSelf selector:@selector(timerDidFire:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; // or use |NSRunLoopCommonModes| if you want the timer to fire while scrolling });}- (void) timerDidFire:(NSTimer *)timer{ // This method might be called when the application is in the background. // Ensure you do not do anything that will trigger the GPU (e.g. animations) // See: http://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW47 NSLog(@"Timer did fire");}
0 0
- Scheduled NSTimer后台运行
- iOS 后台运行NSTimer
- iOS后台运行NSTimer
- iOS开发 NSTimer 后台运行
- 程序进入后台运行NSTimer
- NSTimer后台运行并弹出提示
- NSTimer后台运行并弹出提示
- NSTimer计时器后台运行的解决方法
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- OS程序进入后台后仍运行定时器NSTimer
- iOS程序进入后台后仍运行定时器NSTimer
- linux家谱和学习路线推荐
- 大数据挖掘公司介绍
- 深入理解模板 模板元编程
- 中国最美女黑客现身网络
- java_多态总结
- Scheduled NSTimer后台运行
- 继承
- 静态总结
- Servlet&JSP —— JSP隐式对象
- Git使用教程(二)-- Git常用命令
- android fragment
- 51单片机定时器/计数器(1)
- 互联网世界的“人工智能”——探秘“深度学习”的前世今生
- Leetcode: Largest Rectangle in Histogram