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按钮时, ...
随机推荐
- MySql登陆密码忘记了 怎么办?
MySql登陆密码忘记了 怎么办?root密码:连root密码忘记没用root进修改mysql数据库user表咯 root密码: 方法一:MySQL提供跳访问控制命令行参数通命令行命令启MySQL服务 ...
- centos alias命令详解
Alias命令 功能描述:我们在进行系统的管理工作一定会有一些我们经常固定使用,但又很长的命令.那我们可以给这些这一长串的命令起一个别名.之后还需要这一长串命令时就可以直接以别名来替代了.系统中已经有 ...
- 静态链接ffmpeg库注意的问题(转)
1. 最近使用ffmpeg静态库进行链接,提示 undefined reference,排查发现静态库的顺序导致的,静态库对先后顺序有严格要求 2. 使用ffmpeg库时,在链接时,需要加上-lm - ...
- jeecg中的树形控件demo
1.comboTree控件 1.页面方法: <t:comboTree url="jeecgFormDemoController.do?getComboTreeData" va ...
- CSS: body{font-size: 62.5%;}设置原因
参考博客:http://www.cnblogs.com/daxiong/articles/2772276.html 在网页设计中我们经常看见body{font-size: 62.5%;}这样的设置,为 ...
- ImportError: Couldn't import Django.或者提示Django 模块不存在
ImportError: Couldn't import Django. 或者 多版本的python引起的,执行以下命令 即可解决问题 python3是新的版本的python python3 -m ...
- 潜类别模型(Latent Class Modeling)
1.潜类别模型概述 潜在类别模型(Latent Class Model, LCM; Lazarsfeld & Henry, 1968)或潜在类别分析(Latent Class Analysis ...
- VisualStudio2012轻松把JSON数据转换到POCO的代码(转)
VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Essentials 20 ...
- CentOS+Tomcat环境搭建
此次搭建是基于: CentOS release 5.6 (Final) Kernel \r on an \m 搭建准备:http://pan.baidu.com/s/1pLt1lLh apache-t ...
- 【转】楼天城楼教主的acm心路历程(作为励志用)
利用假期空闲之时,将这几年GCJ,ACM,TopCoder 参加的一些重要比赛作个回顾.昨天是GCJ2006 的回忆,今天时间上更早一些吧,我现在还清晰记得3 年前,我刚刚参加ACM 时参加北京赛区2 ...