【读书笔记】GCD-block-后台运行
2015-07-23 21:19
253 查看
当一个app按home键退出的时候,只有最多5秒的时间做一些保存或清理资源的工作。但是调用beginBackgroundTaskWithExpirationHandler方法,可以最多有10分时间在后台运行。我们可以用这个时间来做清理本地缓存,发送统计数据等事情。
AppDelegate.h
AppDelegate.m
参考资料:
http://www.devtang.com/blog/2012/02/22/use-gcd/
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; //后台长久运行 @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask; @end
AppDelegate.m
//当app进入后台的时候 - (void)applicationDidEnterBackground:(UIApplication *)application { [self beingBackgroundUpdateTask]; //需要长久运行的代码 [self endBackgroundUpdateTask]; } #pragma -mark -functions - (void)beingBackgroundUpdateTask { self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundUpdateTask]; }]; } - (void)endBackgroundUpdateTask { [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask]; self.backgroundUpdateTask = UIBackgroundTaskInvalid; }
参考资料:
http://www.devtang.com/blog/2012/02/22/use-gcd/
相关文章推荐
- D. Equivalent Strings
- cf560B一个矩形如何能包含另外俩个矩形
- nyoj 79 拦接导弹
- 关于javaSocket中 Software caused connection abort: recv failed问题
- 使用RESTClient插件数据模拟(GET,POST)提交
- 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
- 区域赛系列一多边形划分
- HDU-1023 Train Problem II(卡特兰数)
- 试说明一级文件索引结构、二级文件索引结构是如何构造的。
- 【读书笔记】GCD-block-后台运行
- RPM校验软件包和系统命令
- C#高级变量_数组
- 将Excel数据表到数据库表
- 编程是一门实践技能,积极动手实验常有意外的收获
- D - 二叉树遍历,推荐
- HDOJ1995汉诺塔V
- (八十三)第三方类库不支持64位处理器的解决方法
- 如何提高打字速度
- NSDate NSSet
- 关于NSURLSession的上传和下载