Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should my iOS game free memory at all when it enters background mode?

There are a couple of games that I want to mention, to show how they handle entering the background mode:

Clash of Clans. It seems it doesn't free any memory at all when it enters background mode, in fact I can open lightweight applications like Notes and get back to the game to continue playing without any noticeable delay, which seems pretty cool to me from the user's perspective.

Game of War. The game immediately falls back to the loading screen and the initialization process starts all over again when it runs inmediately after it enters background mode, it's just like restarting the game, which is very annoying sometimes.

So, in the case of my game, it has an average memory footprint of 25 mb (and it's going to be less with some optimisations), I know the "Free up as much memory as possible" stuff recommended in order to be a good neighbour on the iOS platform, but considering that freeing memory when the game enters background mode could cause my game to have an "annoying" pause when it enters foreground mode...

...should I save the progress and pause the game when entering background mode without freeing up any memory at all, or should I free as much memory as possible and load those resources again when entering foreground mode, with the respective loading pause?

like image 845
rraallvv Avatar asked Jun 22 '14 06:06

rraallvv


People also ask

What happens when app goes to background in iOS?

If your app enters the background to process an important event, process the event and exit as quickly as possible. All state transitions result in UIKit sending notifications to the appropriate delegate object: In iOS 13 and later — A UISceneDelegate object. In iOS 12 and earlier — The UIApplicationDelegate object.

How do I know if an app is running in the background on iOS?

To detect if an iOS application is in background or foreground we can simply use the UIApplication just like we can use it to detect many other things like battery state, status etc. The shared. application state is an enum of type State, which consists of the following as per apple documentation.

How do you run an application continuously running in the background Swift?

First, go to your project's settings and choose the Capabilities tab. You need to enable the Background Modes capability, then check the box marked Background Fetch. This modifies your app's Info. plist file to add the “fetch” background mode that enables all the following functionality.

What does foreground mean in iOS?

Foreground is the opposite of background. Running the application in the foreground means to keep it running as if you were actively using it. For example, when you are watching a video on your iOS device, you are actively engaging in the video application that you may be using.


2 Answers

As you yourself point out (by giving two games as examples that use opposite strategies), there is no unique answer to your question.

I think a proper answer can be worked out in a given case by clearly defining the use cases you want your app to support.

E.g., one use case, as you mention, could be "the user can switch to a lightweight app and go back to the game without unnecessary waits". (UC1) Then, you should also have a list of lightweight apps and reference devices where you want to make sure that the use case is actually satisfied.

On the other hand, you may want to support a slightly different use case: "the user will always come back to the point where she left the game, unless the app was terminated, in which case the app should present the main menu". (UC2)

In the first case, not freeing up could be the best approach; in the second case, you may want to minimise the chance that the app is terminated while the user does another task (lightweight or not), so that she can go back to the place where she left, even though she has to wait for all resources to load.

There might even be cases where an intermediate approach is best, where you free up only a part of memory, so to strike a balance.

And, of course, it would make no sense going for the first use case (UC1) if your app memory footprint is so large that it will be terminated almost immediately after switching to another app on most of the devices. In such case, it might make more sense freeing up memory, so you at least can spare the app load time (vs. resources load time) when you go back to it.

So, in the end, it depends...

like image 57
sergio Avatar answered Sep 21 '22 13:09

sergio


I would save the progress and pause the game when entering the background without freeing up memory, as many people if they need the memory just delete it from the history thing, or the "carousel of cards". Multi tasking is emphasized in ios 7 so you don't want them to restart the game.

for example, somebody wants to text someone in the middle of playing your game. They exit the game and text someone. But then they have to load the game again. Nobody likes this.

like image 35
jakesan700 Avatar answered Sep 22 '22 13:09

jakesan700