What really happen to loaded data when the app becomes in background - ios

I am new to IOS development. I am still confused about data lifecycle inside the UIViewController. If I had a property called "userData" and I identified it as following
let userData : UserData! {
didSet {
// do something
}
}
and I used this data to handle click events later. What could happen if the app went to background then -say after 1 hour- the user reopened the app and found the page still displaying the content, then he clicked a button inside page. I am not sure if userData will be kept or the page lost it when it went to background!?
My app crashes when I come later and open it, but I am not sure if that's because of identifying variables like this or not

The term background is confusing. Apple generally uses it to mean a state where your app is still getting processor time but another app is front-most. You have to ask if you want more than a few seconds of background time when the user hits the home button.
If the user presses the home button and then swaps to another app your app gets a few seconds of background time to save it's state, and then it moves to the "suspended" state, where it is still in memory, but no longer gets processor time.
Once you've been suspended, if the user switches back to your app while it's still in memory you get a resume message and then you continue running, with everything that was in memory still in memory.
However, once you've been suspended your app can be terminated at any time after that without any warning. That's why you have to save your app state when you get suspended.
If you do get terminated then the next time the user selects your app you get re-launched. You are usually expected to restore state and make it look like the app simply picked up where it left off, but it is a cold launch.
If you are crashing after your app is resumed (not terminated) then you might need help debugging that crash.

Related

Questions about app state after tapping on app icon after apps enters suspended state

I’m putting an app into background.
Assuming that I’m not doing anything to keep the app alive in the background, then the app goes through suspended state in a matter of 5 seconds. Right?
What happens if I then tap on the app icon? That’s not suppose to trigger a didFinishLaunch right? It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications. I won't be getting any other callback. Right?
Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon. Right? Does it also persist device restarts but not force-restart?
The only time I won’t be brought back to the screen I was at (before hitting home) is if the device receives a memory warning and my app is flushed out of suspended state. At this point tapping on the app icon will result in didFinishLaunch. Right?
(I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase. Most of the time it just goes back to its previous screen)
I've already seen Will ios terminate the app running in background after a specific time? but that doesn't address all aspects I want.
It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications.
Right, if your app was not terminated in the background.
I won't be getting any other callback.
Not necessarily true. If you were summoned to the front by a local notification, for example, you'll also get an event about that.
Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon.
Not necessarily. The app might well be terminated silently in the background.
Does it also persist device restarts but not force-restart?
Absolutely not. How can the app run when the device is off? Shutting down the app terminates every app.
I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase
It's not a matter of time. The watchdog process is constantly combing the suspended apps looking for the ones that take up too much memory so that other apps can run. You must not be surprised if yours is one of them.
You can come back to the front launching from scratch or by coming back to life from suspension; it's the most basic fact of iOS app life! You just need to accept it.
But there are lots of things you can do to reduce your chances of being terminated in the background. Giving up memory-consuming objects as you background is first on the list.

How much time an iOS app is guaranteed to stay in the background without being removed from the memory?

I have a Swift iOS app consisting of 3 view controllers – the initial one with a tableview, the second one with the chosen element and ability to create a new time session and the third one with the actual timer for the session (let’s call it the timer vc).
In this case, the focus is the timer view controller. However, the question is not about the timer itself. It works both in foreground and background mode. The problem is this:
The app goes background. Then over a certain fixed period of time, it receives a local notification – a reminder to go back to the timer vc and save the results. Most of the time, it works fine but sometimes when you go back to the app (through notification), it opens the initial vc with the tableview instead of the timer vc, which seems like the system terminates the background app after a while and then reopens it when you go back through the notification. In this case, all the data from the timer vc are obviously lost since the app logic requires that the user save the data after the session is finished.
What I think, the essence of the problem may have to do with how long the app can stay in the background without being terminated or suspended (If I’m not mistaken, in this state, all the current data associated with the app are removed from the memory). I mean, when you set the notifications to be triggered like every 1, 5 or 10 minutes, everything seems to be fine. However, when the notification is set for 30 minutes or more, the mentioned bug seems to appear from time to time (especially when testing on an actual device instead of a simulator). For the most part, it appears if the device storage is almost full - under 1Gb of free space). Which leads me to believe that this may also be a memory management issue.
How to make this timer vc run in the background as much time as needed? Also, does anybody know how much time an iOS app is guaranteed to stay in the background without being removed from the memory? Thanks a lot.
There is no guarantee of your's app execution time when it is in background mode. The operating system can suspend it anytime, Operating system has the responsibility only to notify your app which you will receive in the app delegate.
For setting up the local notification, you need to get the help from OS only. You can't rely on your app's execution in the background thread. For scheduling and handling of local notifications, you can go through the following documentation.
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html#//apple_ref/doc/uid/TP40008194-CH5-SW1

iOS: Determine if another app becomes active

My app needs the device location with an accuracy of about 10 meters. If the app is launched, it usually takes e.g. 10 sec to get the required accuracy. This delay is OK. However when the app is „in use“ (see below), the delay should be less, e.g. 1 sec.
The problem is the following:
When the user switches off the display, the app transits from the active state to the background state (and the delegate methods applicationWillResignActive and applicationDidEnterBackground are called).
Normally, location updates are not done in background. So, next time the app transits from background to active state (delegate methods applicationWillEnterForeground and applicationDidBecomeActive are called), the location manager needs again e.g. 10 sec to reach the required accuracy. This delay in unfortunately not OK.
To avoid it, the app could do location updates in the background. This works fine.
The disadvantage is that these background location updates continue, even if the app is no longer used, because the user pressed the home button and switched to another app. This is disturbing at least for 2 reasons: The GPS hardware is unnecessarily active and uses power, and the user is notified that my app is using the device location although this is no longer required.
My question thus is:
Is it possible to determine if another app becomes active?
If so, background location updates could be switched off.
Is it possible to determine if another app becomes active?
No, your app only knows if it's in background or in foreground handling delegate events. Know if user opened another app or is in the home screen is not possible.
These background location updates continue, even if the app is no longer used, because the user pressed the home button and switched to another app.
I think you're right. You could start a timer when application goes in background and update localization only for an estabilished period (one minute?). Then, at the end of timer count save last localization coordinates. Timer is used to avoid unnecessary updates, then if user doesn't open again your app after a reasonable time, maybe it's because he's using other apps or he locked the device.
When the app comes to the foreground again, if timer is still active you're good. Otherwise show last localization saved and show a small "banner" to advise user that localization could not be accurated for the first 10 seconds (until when required accuracy has been reached)

The applicationWillTerminate method is called always

I am in the middle of development of an iOS OpenGL based game. So far I was not very concerned with the app states change and life cycle - background, foreground, suspended, terminated etc. Now I wanted to add support to moving the app into a suspended mode (after pressing home) and then nicely recuperate it after restarting it.
My problem is that after pressing home button, the app always gets terminated (the application delegate always reaches -applicationWillTerminate:)
The documentation mentions that in order to prevent termination it is important to clear as much memory as possible, stop any timers etc. Doing this did not help to prevent termination, so I tried to experimentally copy the app delegate form an Xcode template app, with completely NO initialization of my app objects after launch/activation, no storyboard, nothing special in main.m.
While the template (I used an Xcode game template) is never terminated after pressing home, it ends at -applicationDidEnterBackground:, my app despite of the fact that is uses less memory and starts completely no tasks is always terminated.
What may be the reason. Included but inactive frameworks ? I have no clue.

App restarts when woken from background

G'day iOS Guru's,
I have searched extensively for an answer, but can't find one (I bet the first response to my question will be to another similar question, but I cant find it).
Anyway, my problem is that I am running a simple map app that the user can drop pins on the map with a customised circle overlay around the dropped pin.
When the app goes into the background (iphone locked or home button pressed), if I re-enter the app within ~ 5 mins, the pins are still there and the app reopens to the last screen.
All good.
However, if I leave the app in the background for longer than 5 mins, the app restarts and all the pins are lost.
I have "Application does not run in background = NO" in the plist, and also enabled "App registers for location updates" under Required background modes.
How can I prevent the app from restarting after it enters the background and load the last opened view?
iOS can, and will, terminate your app while it's in the background if it needs additional resources to carry out whatever's going on in the foreground.
You need to ensure that your data is saved/archived when your app is terminated, and unpacked when re-launched so as to go back to where the user last was. The traditional way to do this is to use the applicationDidEnterBackground method, which is called when your app is suspended. You can then save all the data you need in order to resume cleanly in case your app is later terminated.
However, if you're targeting iOS 6 and upwards you can take advantage of a new feature - State Presumption & Restoration (link to documentation). State restoration off-loads some (but not all) of the heavy lifting onto iOS, and it can automatically snapshot your UI and provides easier ways to preserve and restore data.
in your AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// Save your data
}

Resources