Confusion between background vs suspended app states
Asked Answered
F

3

12

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?

Fiberboard answered 26/2, 2013 at 10:24 Comment(1)
App Life cycle link developer.apple.com/documentation/uikit/app_and_environment/…Suomi
O
11

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.

Olimpia answered 26/2, 2013 at 15:23 Comment(2)
Thanks. Can you provide Link to relevant documentation?Spastic
Documentation link please?Kumquat
N
4

Background:

  • Executing code - Code is executed while the app is in a background state.
  • An app must go into a background state before it can go into a suspended state.
  • ie. Suppose you're on Facebook and you upload a video and you quickly switch to another app immediately after pressing the POST button. Although you switched to another app w/o terminating the app, the app can be configured to perform background processes to complete the upload.
  • An app that is in background does not necessarily mean it is suspended but an app that is suspended is in background.
  • An app can request to stay in background mode for extra time (for purposes of playing audio in the background or completing a network request, for example); afterward it will either go to suspended state or it will be forcibly terminated by the system.

Suspended:

  • Not executing code - Code is not executed while the app is in a suspended state.
  • An app that is in a suspended state is also in a background state.
  • The system moves apps to this state w/o notifying you beforehand.
  • An app in a suspended state is still in memory.

Some additional information that may help you on this topic:

Neely answered 12/3, 2019 at 9:27 Comment(0)
P
3

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.

Phemia answered 6/4, 2016 at 13:10 Comment(1)
just refer this link :codingexplorer.com/execution-states-swift-ios-app explained much moreHuffish

© 2022 - 2024 — McMap. All rights reserved.