0

I am working on state preservation. I would like to preserver the state of my app if

1.. when it is in background, it is killed by IOS due to the lack of memory

I don't want to preserv the state if

2.. when it is in background, it is killed by user

3.. when it is in foreground, it is killed by user

As mentioned by @matt, 1 and 3 can be easily distinguished: when an app is in foreground, if it is killed by user, the state will not be preserved; The app's state is preserved at the moment it goes into background.

My question is: when an app is already in the background, how to determine if it is killed by IOS or by user.

ukim
  • 2,395
  • 15
  • 22
  • How would a user kill the app directly anyway? Do you mean by using double-tap on Home and then swiping upwards? – trojanfoe Jul 22 '14 at 18:09
  • @trojanfoe YES. Won't this kill the app? – ukim Jul 22 '14 at 18:10
  • Yes - I am trying to understand what you mean. I doubt it's possible other then relying on memory warning missing or something, which isn't very solid. – trojanfoe Jul 22 '14 at 18:11
  • 1
    http://stackoverflow.com/questions/7022357/distinguish-between-an-iphone-app-crashing-and-being-killed – Satheesh Jul 22 '14 at 18:13

1 Answers1

2

The decision as you put it is an impossible one. If the app is killed due to lack of memory in the foreground, or if the user summarily kills the app in the background by upward swiping in the app switcher, then state cannot be preserved; this is a crash, and state is removed. If the app is killed due to lack of memory in the background, then the preservation of state happened long ago, i.e. when the app went into the background.

Thus, if you don't preserve state when the app goes into the background ("is closed by user"), it will never be preserved.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for your answer. So if the app is in the background and then closed by user by upward swiping, the sate will be preserved, right? – ukim Jul 22 '14 at 18:16
  • Only if you saved stated prior to going into the background. And that is identical to if you're terminated in the background (low memory, device reboot, etc.) – Rob Napier Jul 22 '14 at 18:18
  • 2
    No. If the user swipes upward, the system takes this as a form of crash and deliberately deletes the state that was previously preserved. The thinking apparently is: if the user does this, there's a problem, so let's not risk an infinite loop (crash on startup) by preserving something that might be the cause of the issue. – matt Jul 22 '14 at 23:25
  • @matt Thank you!! This is want I want to know. By the way, I saw your book on your website. Does your book include what you mentioned here in your answer? I would like to find a source to learn this kind of knowledge. – ukim Jul 23 '14 at 03:53
  • It does not mention this particular fine point, but I got it from the horse's mouth - Apple explains this in a WWDC video on this topic (I forget which one, sorry). – matt Jul 23 '14 at 03:57
  • Here it is documented, at the very very very end of this page: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StatePreservation/StatePreservation.html – matt Jul 23 '14 at 21:20