Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to tasks in dispatch queues when an app enters inactive/background/suspended states in iOS?

Tags:

I've been scouring Apple's documentation on application states and Grand Central Dispatch, but I haven't found a good answer to this question.

According to Apple's documentation, on iOS 4.0:

The application is in the background but is not executing code. The system moves an application to this state automatically and at appropriate times. While suspended, an application is essentially freeze-dried in its current state and does not execute any code. During low-memory conditions, the system may purge suspended applications without notice to make more space for the foreground application.

So assuming the system does not purge a suspended application (suspended -> not running transition), what happens to tasks that are currently executing in a dispatch queue? The phrase "Essentially freeze-dried" leaves much to be desired - exactly what is freeze dried?

My interpretation is whichever GCD queues an app is using at time of suspension will need to be reinstated when the app transitions back to active state; under this interpretation, the tasks existing in pre-suspension GCD queues would disappear. Is my interpretation correct?

like image 505
Rob Avatar asked Jul 31 '11 13:07

Rob


People also ask

What is suspended state in iOS?

When the app is in the background state, the iOS will suspend all activities of this app before iOS 13.5. So there should be no more log messages. But after several switching between the foreground state and background state, iOS stops moving this app to the suspended state, so the app keeps running in the background.

Can iOS apps run in the background?

No. Applications cannot run in the background for over 10 minutes, except for a few certain situations (VOIP, playing audio, etc.) An iOS app which plays audio will keep playing audio indefinitely in the background so long as the audio is playing.

How long will an iOS app run in the background?

The answer is simply 600 seconds (10 minutes), reason is provided by the article above.


1 Answers

When an app is suspended, the entire process is frozen. You can count on the process resuming as if nothing happened at all once it is resumed. Your apps's GCD logical queues don't go away, they remain as they were in memory. And the threads GCD had created in your process to service your queues are resumed in place as if nothing happened as well.

So your interpretation is incorrect: tasks existing in pre-suspension GCD queues do not disappear upon resumption. They never went away; they were only paused.

like image 114
Ryan Avatar answered Oct 10 '22 22:10

Ryan