Executing a Finite-Length Task in the Background
【Executing a Finite-Length Task in the Background】
Apps that are transitioning to the background can request an extra amount of time to finish any important last-minute tasks. To request background execution time, call the beginBackgroundTaskWithName:expirationHandler: method of the UIApplication class. If your app moves to the background while the task is in progress, or if your app was already in the background, this method delays the suspension of your app. This can be important if your app is performing some important task, such as writing user data to disk or downloading an important file from a network server.
The way to use the beginBackgroundTaskWithName:expirationHandler: method is to call it before starting the task you want to protect. Every call to this method must be balanced by a corresponding call to the endBackgroundTask: method. Because apps are given only a limited amount of time to finish background tasks, you must call endBackgroundTask: before time expires or the system will terminate your app. You can use the expiration handler you passed to beginBackgroundTaskWithName:expirationHandler: to end the task. (Use the backgroundTimeRemaining property of the app object to see how much time is available.)
// Listing 3-3 Starting a background task at quit time
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// 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;
});
}
Executing a Finite-Length Task in the Background的更多相关文章
- UWP -- Background Task 深入解析
原文:UWP -- Background Task 深入解析 1. 重点 锁屏问题 从 Windows 10 开始,用户无须再将你的应用添加到锁屏界面,即可利用后台任务,通用 Windows 应用必须 ...
- HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc
Posted by Shiv Kumar on 23rd February, 2011 The Asynchronous Programming Model (or APM) has been aro ...
- C#线程篇---Task(任务)和线程池不得不说的秘密(5)
在上篇最后一个例子之后,我们发现了怎么去使用线程池,调用ThreadPool的QueueUserWorkItem方法来发起一次异步的.计算限制的操作,例子很简单,不是吗? 然而,在今天这篇博客中,我们 ...
- C# Task WhenAny和WhenAll 以及TaskFactory 的ContinueWhenAny和ContinueWhenAll的实现
个人感觉Task 的WaitAny和WhenAny以及TaskFactory 的ContinueWhenAny有相似的地方,而WaitAll和WhenAll以及TaskFactory 的Continu ...
- C# Task WaitAll和WaitAny
Task 有静态方法WaitAll和WaitAny,主要用于等待其他Task完成后做一些事情,先看看其实现部分吧: public class Task : IThreadPoolWorkItem, I ...
- C#线程篇---Task(任务)和线程池不得不说的秘密
我们要知道的是,QueueUserWorkItem这个技术存在许多限制.其中最大的问题是没有一个内建的机制让你知道操作在什么时候完成,也没有一个机制在操作完成是获得一个返回值,这些问题使得我们都不敢启 ...
- 使用Task实现非阻塞式的I/O操作
在前面的<基于任务的异步编程模式(TAP)>文章中讲述了.net 4.5框架下的异步操作自我实现方式,实际上,在.net 4.5中部分类已实现了异步封装.如在.net 4.5中,Strea ...
- Windows10 Dev - Background Execution
The Universal Windows Platform (UWP) introduces new mechanisms, which allow the applications to perf ...
- iOS的后台任务
翻译自:http://www.raywenderlich.com/29948/backgrounding-for-ios (代码部分若乱码,请移步原链接拷贝) 自ios4开始,用户点击home按钮时, ...
随机推荐
- 去掉PowerDesigner生成SQL脚本中字段名带的引号
使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号.其实加引号是PL/SQL的规范,数据库会严格按照“”中的名称建表,如果没有“”,会按照ORACL ...
- Linux下用文件IO的方式操作GPIO(/sys/class/gpio)(转)
通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...
- 机器学习The Learning Problem——coursera简要总结
1.人类及动物的学习模式:观察->学习->技能 机器学习的模式:data->ML(机器学习)->skill 2.那什么是skill:技能是某种表现方法的增进 eg:stac ...
- emacs之配置4,颜色插件
来自https://github.com/oneKelvinSmith/monokai-emacs/blob/master/monokai-theme.el monokai-theme.el ;;; ...
- JavaWeb的过滤器Filter
Filter的作用是在请求到达web资源(HTML.css.Servlet.Jsp)之前拦截,做一个预处理. 创建一个类实现Filter接口,在web.xml中对文件进行配置 <filter&g ...
- [C#][控件]WebBrowser 使用范例
if (webInfo.Document != null) webInfo.Document.OpenNew(true); else webInfo.Navigate("about:blan ...
- Linux操作系统-基本命令(二)
Linux操作系统基本命令 文件操作类命令 – ln命令 另外一种链接方式称为符号链接(软链接),是指一个文件指向另外一个文件的文件名.软链接类似于Windows系统中的快捷方式.软链接由ln -s命 ...
- sorl 6.6.0 定时更新索引
solr 定时更新索引 – solr 6.6.0 – dataimport.scheduler 这里先重点说下,定时更新引用的org.apache.solr.handler.dataimport.sc ...
- Discuz 任务、道具、任务和验证类插件制作
一.广告类脚本位置:source/class/adv/adv_name.php语言包位置:source/language/adv/lang_name.php <?php class adv_na ...
- Liunx cannot remove `xxx': Operation not permitted
链接: http://mangocool.com/detail_1_1439515930283.html 解到原来文件还可以设置隐藏权限,就是这个chattr设置,下面我们来看看这个命令的详解. [r ...