iOS app applicationWillEnterForeground and it stuck for a while

时光总嘲笑我的痴心妄想 提交于 2019-12-05 18:52:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10648388/ios-app-applicationwillenterforeground-and-it-stuck-for-a-while

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!