Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion between background vs suspended app states

I am bit confused about these two states. Following is my understanding;

when app is in background and if you have "Application does not run in background" set to NO in App plist file then App continues running in background. In suspend mode App stays in memory but does not execute code! System doesn't notify App when it moves to Suspended state and wipes out App from memory in low memory situation to create space for foreground Apps.

Is my understanding correct? If it is so then why don't I get my NSLog printed on console when App running in background and executing code? What actually happens to my TCP socket connection where I am continuously gets the data from server? Why do we need Suspended state as anyway System eventually wipes out App from memory in low memory situation. Is there any difference between App becoming active from suspended state or starting fresh?

like image 427
AlienMonkeyCoder Avatar asked Feb 26 '13 10:02

AlienMonkeyCoder


2 Answers

You should look in the IOS App Programming Guide section "App States and Multitasking". Find that by searching the Xcode documentation with the phrase "App States and Multitasking" and "Hits Must" item set to "Match Search Term". Another useful search phrase: "background execution".

The summary answer is that an application can continue "executing" in the background indefinitely for only a limited number of reasons:

in iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Newsstand apps that need to download and process new content
  • Apps that receive regular updates from external accessories

Aside from those specific operations, an app can ask to continue to execute for a very short time which the documentation covers in the section "Executing a Finite-Length Task in the Background". After a short time either your app tells the system it is done (and is then suspended) or it is forcibly terminated. Details in the doc.

Another useful bit of that document, with nice state diagrams, is the section "Managing App State Changes". That section talks about going into the background and returning to the foreground. It should answer your question about the difference between starting fresh versus starting from the suspended state. The short (not quite correct) answer is that if you start from the suspended state and you don't take any special actions when entering the background or (re)entering the foreground then you just continue more-or-less from where you were. Also, starting from the suspended state is faster. Read the doc since it says it much better than my paraphrase would.

like image 132
Charlie Price Avatar answered Sep 17 '22 18:09

Charlie Price


App State

Not running: Your app is in this state before it starts up.

Active: Once your app is started, receiving events.

Inactive: When your app is running but something happens to interrupt it, like a phone call, it becomes inactive. Inactive means that the app is still running in the foreground but it’s not receiving events.

Backgrounded: In this state, your app is not in the foreground anymore but it is still able to run code.

Suspended: Your app enters this state when it’s no longer able to run code.

like image 25
Avijit Nagare Avatar answered Sep 20 '22 18:09

Avijit Nagare