Submitted GCD operation does not start when application is placed in background

I run the method essentially an infinite loop using dispatch_queue_create

and then dispatch_async

(and then the code loop is inside the block sent).

The loop works fine. However, when the application receives help information, it pauses. Then it restarts when the application takes the foreground.

How can I prevent this? I have searched here , but it seems that priority is not one of the things I can choose.


source to share

1 answer

Use the - [UIApplication beginBackgroundTaskWithExpirationHandler:] method to start the background task. The OS will give you ten minutes and will call the expirationHandler block when it ends.

This method will return UIBackgroundTaskInvalid if the device is unable to run the code in the background, or the task ID you should use to end it otherwise.

You can (and should) end it early by calling - [UIApplication endBackgroundTask];

Maybe you can start the task at the beginning of your block and finish it when it finishes, if it's an infinite loop, just start the task in the applicationWillResignActive method and end it on applicationDidBecomeActive. But remember that you only have ten minutes to have more time for your application to have to use location, audio or voip.



All Articles