App state notifications seem out of sync with -[UIApplication applicationState]

Originator:andrewh
Number:rdar://12892621 Date Originated:17-Dec-2012 11:15 AM
Status:Open Resolved:
Product:iOS SDK Product Version:6.0, 6.0.1
Classification:Serious Bug Reproducible:Rarely
 
Summary:

UIApplicationWillEnterForegroundNotification received after applicationState != UIApplicationStateBackground test passed

I am writing an Objective-C wrapper for using a GCD timer source. One of the goals is for the wrapper to manage suspending the timer source when the application enters the background.

The docs for -[UIApplicationDelegate applicationDidEnterBackground:] indicate that it should be used to invalidate timers, among other things; I'm interpreting this somewhat loosely (perhaps that is the source of my problem?) in that I'm suspending a GCD timer (dispatch_suspend()) in response to UIApplicationDidEnterBackgroundNotification).

When an instance of the wrapper is initialized, it checks [UIApplication sharedApplication].applicationState != UIApplicationStateBackground to determine whether the GCD timer source should initially be resumed as part of the initialization sequence. It also registers for UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification which it uses to suspend and resume the timer source (respectively).


Steps to Reproduce:

unknown


Expected Results:

If during initialization [UIApplication sharedApplication].applicationState != UIApplicationStateBackground is true, then the next notification received should not be UIApplicationWillEnterForegroundNotification.


Actual Results:

The issue that I have observed is that there seem to be cases where the check of [UIApplication sharedApplication].applicationState != UIApplicationStateBackground during initialization is passed so that the timer source is resumed, and the next notification that is received is UIApplicationWillEnterForegroundNotification which causes the timer source to be resumed a second time. This leads to a crash since the timer was not suspended at the time of the second resume.

I can work around this by keeping track of the application state locally and confirming the transitions that would be duplicate, but I am concerned that I may be doing something incorrectly or that there may be a bug (either in implementation or in documentation).


Regression:


Notes:

Supposing that this was a problem in my code, have posed this as a discussion question on StackOverflow, but have not received a satisfactory answer. http://stackoverflow.com/questions/13752197/uiapplicationwillenterforegroundnotification-received-after-applicationstate

Please advise whether this is a case of me doing something incorrectly, or whether there might be a bug in iOS or an error in the documentation.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!