iOS app applicationWillEnterForeground and it stuck for a while
Asked Answered
A

1

9

I add this function to post a notification when the app enter foreground:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName: @"UIApplicationWillEnterForegroundNotification" object: nil];
}

In my own class:

- (void) handleEnterForeground: (NSNotification*) sender
{
    [self reloadTableData];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnterForeground:)
                                             name: @"UIApplicationWillEnterForegroundNotification"
                                           object: nil];
}

but the handleEnterForeground: function will called twice, I don't know why. The reloadTableData: function will call remote webService , so when the app enter foreground, it will stuck for a while.

Anallise answered 18/5, 2012 at 7:24 Comment(0)
B
17

The system will call that event automatically. The reason it fires twice is because you manually fire it again.

P.S. It's better to use the variable name UIApplicationWillEnterForeground, instead of a NSString literal.

EDIT: I realize now the confusion is coming from the fact that you didn't know that this even name was already taken. As a note to other people who run into this kind of problem, it is a good practice to prefix your event names with your project prefix (i.e. XYZEventNotification) to avoid collisions.

Babiche answered 18/5, 2012 at 7:33 Comment(7)
Im so sure I called that function once.Anallise
@cnu he is firing it once, and iOS runtime is firing it once. This event gets called automatically.Babiche
Thank you , it's OK now!!! I change "UIApplicationWillEnterForegroundNotification" to an other word .Anallise
Instead of changing it to another word...why not just take out that code, and use the event that comes automatically?Babiche
His name is borrrden, not OLO.Chloral
I have the same issue, no double registration with the samen name, 100% sure. The only solution is to post notification with a unique name in AppDelegate's applicationWillEnterForeground or applicationDidBecomeActive.Airway
"After calling this method, the app also posts a UIApplicationDidBecomeActiveNotification notification to give interested objects a chance to respond to the transition." according to Apple. Well I get 2 notifications after each other.Airway

© 2022 - 2024 — McMap. All rights reserved.