Ryan
Ryan

Reputation: 518

dispatch_async vs beginBackgroundTaskWithExpirationHandler

I need to implement a background process in an iOS app, that performs a job every 60 seconds. I know this can only be done while the app is in focus, but I do want this job to finish running in the background after the app is closed. I will use GCD to dispatch a timer that calls my job asynchronously every 60 seconds using either dispatch_async or beginBackgroundTaskWithExpirationHandler.

I am wondering which method is the best, or if they are essentially the same. Is it okay to use beginBackgroundTaskWithExpirationHandler to execute a job even while the app is in the foreground? Or am I better off trying to cancel the job when the app state changes, and then restart the job as a background task?

Upvotes: 1

Views: 750

Answers (1)

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

-beginBackgroundTaskWithExpirationHandler: doesn't run code on a background thread/queue. It tells the OS that you are going to continue doing work when your app is not active.

The two serve completely different purposes.

Upvotes: 2

Related Questions