When exactly does the iOS system terminate/kill your app?

I'm well aware of the docs from apple about how the system can terminate your app because of the following



1. Needs memory

2. Your app is misbehaving



So regarding point number 1 I was wondering if it's possible that the system kills my app if the following happens


1. My app is launched and is currently in the foreground

2. User presses the lock button app resigns active

3. Phone is currently locked but when the phone is unlocked it should come back to my app (assuming the user does not open a notification or try to navigate to a specific app on the lock screen, but just unlocks their phone).


Is it possible that my app can actually be terminated before/while the user unlocks the phone? Should the phone not come back to the app given these steps? As far as I know doesn't the system kill apps because of the above points? (needs space, app misbehaves). But if the device is asleep I would assume it's not in need of too much resources. Another point is that If it was to kill an app for more memory or what not, would it not kill a different app that has been in memory longer or since my app was the last app opened? Or does it not prioritize how it purges apps at all?



Regarding point 2 I was wondering what EXACTLY constitutes as app misbehaving?



Thanks in advance!

1. Needs memory

2. Your app is misbehaving

3. Taking too long/not responding/stuck in a loop/timing out

When exactly does the iOS system terminate/kill your app?
 
 
Q