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的更多相关文章

  1. UWP -- Background Task 深入解析

    原文:UWP -- Background Task 深入解析 1. 重点 锁屏问题 从 Windows 10 开始,用户无须再将你的应用添加到锁屏界面,即可利用后台任务,通用 Windows 应用必须 ...

  2. HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc

    Posted by Shiv Kumar on 23rd February, 2011 The Asynchronous Programming Model (or APM) has been aro ...

  3. C#线程篇---Task(任务)和线程池不得不说的秘密(5)

    在上篇最后一个例子之后,我们发现了怎么去使用线程池,调用ThreadPool的QueueUserWorkItem方法来发起一次异步的.计算限制的操作,例子很简单,不是吗? 然而,在今天这篇博客中,我们 ...

  4. C# Task WhenAny和WhenAll 以及TaskFactory 的ContinueWhenAny和ContinueWhenAll的实现

    个人感觉Task 的WaitAny和WhenAny以及TaskFactory 的ContinueWhenAny有相似的地方,而WaitAll和WhenAll以及TaskFactory 的Continu ...

  5. C# Task WaitAll和WaitAny

    Task 有静态方法WaitAll和WaitAny,主要用于等待其他Task完成后做一些事情,先看看其实现部分吧: public class Task : IThreadPoolWorkItem, I ...

  6. C#线程篇---Task(任务)和线程池不得不说的秘密

    我们要知道的是,QueueUserWorkItem这个技术存在许多限制.其中最大的问题是没有一个内建的机制让你知道操作在什么时候完成,也没有一个机制在操作完成是获得一个返回值,这些问题使得我们都不敢启 ...

  7. 使用Task实现非阻塞式的I/O操作

    在前面的<基于任务的异步编程模式(TAP)>文章中讲述了.net 4.5框架下的异步操作自我实现方式,实际上,在.net 4.5中部分类已实现了异步封装.如在.net 4.5中,Strea ...

  8. Windows10 Dev - Background Execution

    The Universal Windows Platform (UWP) introduces new mechanisms, which allow the applications to perf ...

  9. iOS的后台任务

    翻译自:http://www.raywenderlich.com/29948/backgrounding-for-ios (代码部分若乱码,请移步原链接拷贝) 自ios4开始,用户点击home按钮时, ...

随机推荐

  1. python try except, 异常处理

    http://www.runoob.com/python/python-exceptions.html http://blog.sciencenet.cn/blog-3031432-1059523.h ...

  2. 在服务器上运行db:seed数据填充时,出错的问题解决

    在服务器上运行db:seed数据填充时,出错的问题解决 运行composer  dump-autoload

  3. Windows Server Core Command (管理服务器核心的具体操作命令)

    从 Windows Server 2008 开始,管理员可以选择安装具有特定功能但不包含任何不必要功能的 Windows Server 的最小安装服务器核心(Server Core),它为一些特定服务 ...

  4. LogHelp 日记分天记录,只记30天日记

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  5. 再记录一次delete出错的经历

    调试的时候进行到delete语句时出现问题,我做的操作是在函数体内用int*申请了N个内存空间,这让我十分纳闷,为什么不能delete呢? 回忆到之前delete出错也遇过一次问题 手动封装OpenC ...

  6. Unicode化

    为了程序编写方便,根除乱码问题等等需求,很多新项目都采用了Unicode编码.同时,不少使用MBCS多字节编码的旧项目为了升级,也有了转向Unicode编码的意向.不过,从MBCS升级到Unicode ...

  7. Webpack-simple cross-env 不是内部或外部命令问题处理

    本文转载自:https://www.cnblogs.com/stono/p/6984222.html Webpack-simple cross-env 不是内部或外部命令问题处理 学习了:https: ...

  8. MMO技能系统的同步机制分析

    转自:http://www.gameres.com/729629.html 此篇文章基于之前文章介绍的技能系统,主要介绍了如何实现MMO中的技能系统的同步.阅读此文章之前,推荐首先阅读前一篇文章:一个 ...

  9. jsp 学习 第1步 - 引入 jstl

    通过 eclipse 新建 动态web项目  默认是没有引入 jstl, 则无法JSP页面引入相关标记. <%@ taglib prefix="c" uri="ht ...

  10. FireDAC 之FDMetaInfoQuery

    FDMetaInfoQuery http://docs.embarcadero.com/products/rad_studio/firedac/frames.html http://docwiki.e ...