Apple’s documentation recommends installing a keep-alive handler in the applicationDidEnterBackground: method. It also says that the new handler replaces the previously installed one.
So my question is, why do they recommend installing it in applicationDidEnterBackground? Do I have to install this handler every time the application enters background? In other words, is the handler discarded every time you enter foreground, so you have to re-install it every time you enter background?
If not, is there a more appropriate place to install this handler?
I can hazard a guess – there are a number of conceivable scenarios where you may want to change the handler depending on user settings or application behaviour. In that case, it makes the most sense to set it up in
applicationDidEnterBackground. There is perhaps also something to be said for plain consistency between applications and codebases.But you can certainly install your keep-alive handler elsewhere in your app, and it shouldn’t (to my knowledge) be discarded upon entering the foreground again. However, keep-alive handlers are discarded upon application termination.
Since all that call actually does is basically set a few variables, there’s no real performance penalty to calling it in
applicationDidEnterBackground, so it won’t disrupt the process of pushing an app to the background.