UIApplicationWillEnterForegroundNotification received after applicationState! = UIApplicationStateBackground test passed

I am writing an Objective-C wrapper to use a GCD timer source. One goal is that the shell controls the suspension of the timer source when the application enters the background.

docs for -[UIApplicationDelegate applicationDidEnterBackground:] indicates that it should be used, for example, to cancel timers; I interpret this a bit (maybe this is the source of my problem?) In that I pause the GCD timer ( dispatch_suspend() ) in response to UIApplicationDidEnterBackgroundNotification ).

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

The problem I observed is that there seem to be cases where the [UIApplication sharedApplication].applicationState != UIApplicationStateBackground during initialization so that the timer source resumes and the next UIApplicationWillEnterForegroundNotification notification that causes the timer source to resume second time. This results in a failure because the timer was not paused during the second resume.

I can get around this by monitoring the status of the application locally and confirming transitions that would be duplicates, but I am worried that I might be doing something wrong or that there might be an error (either in the implementation or in the documentation).

+4
source share
3 answers

Here's how the app works:

At initial launch

  • Application: didFinishLaunchingWithOptions
  • applicationDidBecomeActive

When you press the home button,

  • applicationWillResignActive
  • applicationDidEnterBackground

When opening a background application

  • applicationWillEnterForeground
  • applicationDidBecomeActive

Opening - (initial launch and opening of the background application) - applicationDidBecomeActive receives a call - here the timer is initialized.

Hiding or receiving a call, etc. - the applicationWillResignActive call is called here - the timer stops here.

Hope this helps.

+2
source

From the UIApplication docs:

 UIApplicationStateInactive The application is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the application is transitioning to or from the background. 

So, it is possible that your application was in Background, will be in Foreground soon, but is currently moving.

0
source

So here is what you can do to better understand what is going on:

In your main.m applications, add the following:

  @interface GTTestObject : NSObject - (void)logNotification:(id)sender; @end @implementation GTTestObject - (void)logNotification:(id)sender { NSLog(@"%@", [(NSNotification *)sender name]); } @end int main(int argc, char *argv[]) { @autoreleasepool { // I'm assuming you'd be using ARC... GTTestObject *obj = [[GTTestObject alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:obj selector:@selector(logNotification:) name:nil object:nil]; return UIApplicationMain(argc, argv, nil, NSStringFromClass([GTAppDelegate class])); } } 

Then for each method MyAppDelegate.m "... didFinishLaunching, ... didEnterBackground, etc." add this:

  NSLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd)); 

Now you can see the sequence, timestamp, and (possibly) any conflicts with notification / delegate calls that occur simultaneously.

You might get slightly different test results for testing, so run it a few times to be sure.

To run the example, I see this every time:

  2012-12-21 08:13:58.559 Gamerton[34158:c07] [GTAppDelegate application:didFinishLaunchingWithOptions:] state: UIApplicationStateInactive 2012-12-21 08:13:58.560 Gamerton[34158:c07] UIApplicationDidFinishLaunchingNotification state: UIApplicationStateInactive ... 2012-12-21 08:13:58.561 Gamerton[34158:c07] [GTAppDelegate applicationDidBecomeActive:] state: UIApplicationStateActive 2012-12-21 08:13:58.561 Gamerton[34158:c07] UIApplicationDidBecomeActiveNotification state: UIApplicationStateActive ... Hit home button 2012-12-21 08:16:08.227 Gamerton[34170:c07] [GTAppDelegate applicationWillResignActive:] state: UIApplicationStateActive 2012-12-21 08:16:08.228 Gamerton[34170:c07] UIApplicationWillResignActiveNotification state: UIApplicationStateActive 2012-12-21 08:16:08.229 Gamerton[34170:c07] UIApplicationSuspendedNotification state: UIApplicationStateBackground 2012-12-21 08:16:08.229 Gamerton[34170:c07] [GTAppDelegate applicationDidEnterBackground:] state: UIApplicationStateBackground ... Reopen app 2012-12-21 08:16:59.364 Gamerton[34170:c07] [GTAppDelegate applicationWillEnterForeground:] state: UIApplicationStateBackground 2012-12-21 08:16:59.365 Gamerton[34170:c07] UIApplicationWillEnterForegroundNotification state: UIApplicationStateBackground 2012-12-21 08:16:59.365 Gamerton[34170:c07] _UIApplicationDidRemoveDeactivationReasonNotification 2012-12-21 08:16:59.366 Gamerton[34170:c07] [GTAppDelegate applicationDidBecomeActive:] state: UIApplicationStateActive 2012-12-21 08:16:59.366 Gamerton[34170:c07] UIApplicationDidBecomeActiveNotification state: UIApplicationStateActive 2012-12-21 08:16:59.366 Gamerton[34170:c07] UIApplicationResumedNotification state: UIApplicationStateActive 2012-12-21 08:16:08.230 Gamerton[34170:c07] UIApplicationDidEnterBackgroundNotification state: UIApplicationStateActive 
0
source

All Articles