0

In the GCM client guide for iOS, it shows you to call [[GCMService sharedInstance] connectWithHandler:...] in -applicationDidBecomeActive:, and later it shows you to call [[GCMService sharedInstance] disconnect]; in -applicationDidEnterBackground:.

The placement of connect and disconnect seems asymmetric to me. Is there a reason it is done this way? I feel that it should either be

  • Connect in -applicationDidBecomeActive: and disconnect in -applicationWillResignActive:, or
  • Connect in -applicationWillEnterForeground: and disconnect in -applicationDidEnterBackground:
user102008
  • 30,736
  • 10
  • 83
  • 104

1 Answers1

0

If you look at the Execution States for Apps in Apple's App Programming Guide for iOS, it states that:

applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.

Compare to

applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.

For example, if you press the Home button of your device twice, the applicationWillResignActive method will be call immediately, your app is in a transitioning state now. see the image below (Your app leave foreground, but not yet in background):

enter image description here

If you select other app, the applicationDidEnterBackground of your app will be called, and your app enters background. So it is better to call GCMService.sharedInstance().disconnect() when your app is actually in a background state compared to a transitioning state.


Similarly, applicationWillEnterForeground vs applicationDidBecomeActive.

If you are in other app now, and you switch back to your own app, the applicationWillEnterForeground will be called immediately, but your app is might still in an inactive state(which is currently not receiving events).

It might only last for less than half a second, and applicationDidBecomeActive will called immediately after. Because applicationWillEnterForeground is more a transitioning state. So it is better to call GCMService.sharedInstance().connectWithHandler() when your app is actually in an active state(app is running in the foreground and is receiving events).


You might think applicationDidBecomeActive and applicationDidEnterBackground are asymmetric, but these two methods all have a word Did. But two other methods have the word Will, which means a transitioning state.

You can also see the Life Cycle picture from this StackOverflow answer.

Edit after @user102008's comment:

If you do connect in the applicationWillEnterForeground method, then connect method wont be called when the app is first launched. Also, the GCM doc says that Call disconnect when the client app is in the background or when it needs to stop exponential background connection retry, so I think its better to call disconnect when the app is in background, instead of when phone call comes? I think you can use enum with 3 different states connecting , not connected and connected, when the applicationDidBecomeActive method is called, you can check if its not connected or connecting, then you call the connect method again.

Community
  • 1
  • 1
ztan
  • 6,861
  • 2
  • 24
  • 44
  • But if you go from active to inactive (e.g. if a dialog pops up, or a call comes, or you press Home twice), and back to active, then you will have done connect twice, with no disconnect in between (because it never went into background). – user102008 Aug 12 '15 at 18:37
  • @user102008, you have a good point, I also edit my answer, because comment only allow certain amount of characters. – ztan Aug 12 '15 at 19:48