iOS app applicationWillEnterForeground并且它停留了一段时间 [英] iOS app applicationWillEnterForeground and it stucked for a while

查看:577
本文介绍了iOS app applicationWillEnterForeground并且它停留了一段时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加此功能以在应用输入前景时发布通知:

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

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

在我自己的班级中:

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


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

但handleEnterForeground:函数将调用两次,我不知道为什么。
reloadTableData:函数将调用远程webService,所以当应用程序输入
前景时,它会停留一段时间。请帮我解决这个问题,谢谢。

but the handleEnterForeground: function will called twice, I dont know why. The reloadTableData: function will call remote webService , so when the app enter foreground, it will stucked for a while.Pls help me with this, and thank you .

推荐答案

系统将自动调用该事件。它发射两次的原因是你再次手动触发它。

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

P.S。最好使用变量名称UIApplicationWillEnterForeground,而不是NSString文字。

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

编辑:我现在意识到混乱来自于你不知道这个甚至名字已经被采取。作为遇到此类问题的其他人的注释,最好在事件名称前加上项目前缀(即XYZEventNotification)以避免冲突。

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.

这篇关于iOS app applicationWillEnterForeground并且它停留了一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆