IOS 后台之长时间任务 beginBackgroundTaskWithExpirationHandler 申请后台十分钟 600秒
2024-08-28 16:49:20
10分钟
beginBackgroundTaskWithExpirationHandler,beginBackgroundTaskWithName
endBackgroundTask
定义变量
UIBackgroundTaskIdentifier bgTask;
- (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ // Clean up any unfinished task business by marking where you // stopped or ending the task outright. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ // Do the work associated with the task, preferably in chunks. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }
Declaration
Swift
func beginBackgroundTaskWithExpirationHandler(_
handler
: (() -> Void)?) -> UIBackgroundTaskIdentifier
Objective-C
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))
handler
Listing - Starting a background task at quit time
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
), ^{
// Do the work associated with the task.
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
^{} block 语法。
dispatch_queue_create
dispatch_release
dispatch_async_f
Queue: dispatch_queue_t; Queue := dispatch_queue_create('Video Capture Queue', );
最新文章
- os模块汇总
- Spring Boot 1 创建Demo
- c语言知识(2)
- linux 交换分区分配规则
- Hibernate事务传播性
- 代码生成引擎之T4模版
- final, finally, finalize 的区别
- DL4NLP——词表示模型(三)word2vec(CBOW/Skip-gram)的加速:Hierarchical Softmax与Negative Sampling
- TestNG的简单使用
- java基础笔记(4)----数组
- python实现购物车
- html5配合css3实现带提示文字的输入框(摆脱js)
- python importlib动态导入模块
- Java进阶篇之十五 ----- JDK1.8的Lambda、Stream和日期的使用详解(很详细)
- [2019.03.16]使用DOM操作函数和CSS选择器来针对已有的HTML进行只凭JS的改动
- Tomcat zabbix监控、jmx监控、zabbix_java_gateway
- Ionic开发遇到的坑整理
- DOM元素加载之前执行的jQuery代码
- spring task 实现定时执行(补充:解决定时任务执行2次问题)
- ISCC2018(misc)