When an iOS application goes to the background, are lengthy tasks paused?

后端 未结 3 1776
有刺的猬
有刺的猬 2020-11-27 13:06

Yes, I know if I wish my app to be responsive to users\' multitasking actions, such as switch to another app, I should deal with

- (void)applicationWillResi         


        
相关标签:
3条回答
  • 2020-11-27 13:55

    If you are doing some operation which might consume time and you don't want to kill it then you can extend the time for your operation by executing in UIBackground Task i

    {
        UIBackgroundTaskIdentifier  taskId = 0;
        taskId = [application beginBackgroundTaskWithExpirationHandler:^{
            taskId = UIBackgroundTaskInvalid;
        }];
    
    // Execute long process. This process will have 10 mins even if your app goes in background mode.
    
    }
    

    The block argument called "handler" is what will happen when the background task expire (10min). Here is a link to the documentation

    0 讨论(0)
  • 2020-11-27 14:04

    From the iOS App Programming Guide:

    Your app delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return. In practice, this method should return as quickly as possible. If the method does not return before time runs out, your app is killed and purged from memory. If you still need more time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request background execution time and then start any long-running tasks in a secondary thread. Regardless of whether you start any background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.

    If the long-running operation you describe above is on the main thread and it takes longer than 5 seconds to finish after your application heads to the background, your application will be killed. The main thread will be blocked and you won't have a chance to return from -applicationDidEnterBackground: in time.

    If your task is running on a background thread (and it really should be, if it's taking long to execute), that thread appears to be paused if the application returns from -applicationDidEnterBackground: (according to the discussion in this answer). It will be resumed when the application is brought back to the foreground.

    However, in the latter case you should still be prepared for your application to be terminated at any time while it's in the background by cleaning things up on your way to the background.

    0 讨论(0)
  • 2020-11-27 14:04

    Like mentioned above, there are a few cases where your app runs in the background and apple can allow or deny depending on what you are doing.

    https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

    More importantly if you do fit into one of these categories your app refresh rate is determined by an apple algorithm that takes into consideration your app usage on that device vs other apps. If your app is used more often then it gets more background time allotted. This is just one variable but you get the idea that background time allocation varies app to app and not under your control.

    0 讨论(0)
提交回复
热议问题