iOS 应用程序 applicationWillEnterForeground 卡住了一段时间

标签 ios uiapplicationdelegate background-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 ,所以当应用进入 前景,它会卡住一段时间。

最佳答案

系统会自动调用该事件。它触发两次的原因是因为您再次手动触发它。

附言最好使用变量名 UIApplicationWillEnterForeground,而不是 NSString 文字。

编辑:我现在意识到困惑来自于您不知道这个偶数名称已经被使用的事实。提醒遇到此类问题的其他人,最好在事件名称前加上项目前缀(即 XYZEventNotification)以避免冲突。

关于iOS 应用程序 applicationWillEnterForeground 卡住了一段时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10648388/

相关文章:

ios - (Swift) 应用内购买 Wi-Fi 错误

ios - Swift 观察 UIApplication 属性

ios - 如何使用 xcode 在 ios 上测试后台到前台?

ios4 - iphone 4 SDK : detect return from background mode

ios - 是否建议将 React Native 应用程序的核心逻辑托管在云中?

objective-c - 我如何在 iphone 应用程序中设置表格 View 单元格属性

ios - NSPersistentContainer 默认创建什么样的持久化存储?

ios - ApplicationWillTerminate NSURLSession 可能吗?

iphone - 是否需要保存对单例 objective-c 对象的引用,以便在 IOS 应用程序的整个生命周期中保存它?

ios - 当应用程序进入后台并进入前台时调用 UIViewController 方法