Executing a Finite-Length Task in the Background
2014-05-21 15:45
731 查看
【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.)
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, 0), ^{ // Do the work associated with the task, preferably in chunks. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }
相关文章推荐
- 【Multitasking】Executing a Finite-Length Task in the Background
- Completing a Long-Running Task in the Background(在后台完成长时间运行的任务)
- server 2 task(s) are sleeping waiting for space to become available in the log segment for database tempdb.
- [20081201]--limit the length of textarea in JavaScript
- How to get the running applications in windows task manager using powershell
- Launching Tasks in the Foreground and Background
- Being productive in the background – background tasks
- Hide a process in the kill task menu
- How do you get the length of a string in jQuery?
- Find the substring of length 3 which is present in the reverse order from the string
- The "GenerateResource" task failed unexpectedly. a generic error occured in GDI+
- Idling along, (or what to do in the idle task)
- Compute the maximal length of the increasing subsequence in an array
- Android ImageLoader - load images sequencially in the background
- Working in the Background
- Invoice Verification in the background
- 高级bash编程一书Example 3-3. Running a loop in the background的问题
- hide the task bar and input panel in Windows Mobile
- Question 9: Which of the following statements correctly describe the results of executing the code below in C++?
- How to get the length of array of strings in C? - Yahoo! Answers