问题描述
我添加了此功能,以便在应用进入前台时发布通知:
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 don't know why. ThereloadTableData: function will call remote webService , so when the app enterforeground, it will stuck for a while.
推荐答案
系统将自动调用该事件.触发两次的原因是您手动再次触发.
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应用程序应用程序WillEnterForeground并停留了一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!