viewWillAppear 和 UIApplicationDidBecomeActiveNotification

发布于 2025-01-02 20:20:55 字数 219 浏览 3 评论 0原文

当应用程序变为活动状态时,我使用 UIApplicationDidBecomeActiveNotification 刷新我的表格视图。我的问题是,在我的 ViewWillAppear 中,我还调用了一个方法来刷新该表的数据。

这导致表在应用程序启动时刷新两次。当应用程序最初启动时,如何才能让其中一个不触发?刷新表需要对网络和本地数据进行一些密集处理。所以我真的希望只执行一次此操作。

谢谢。

I am using the UIApplicationDidBecomeActiveNotification to refresh my tableview when the app becomes active. My problem is that in my ViewWillAppear, I am also calling a method to refresh this table's data.

This is causing the table to be refreshed twice upon application launch. How can I get one of them not to fire when the app is initially launched? Refreshing the table has some intensive processing of network and local data.. so I would really like o only perform this action once.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2025-01-09 20:20:55

您需要使用UIApplicationWillEnterForegroundNotification而不是UIApplicationDidBecomeActiveNotification

后者会在您的应用程序每次激活时发布(首次启动、通话/短信中断后返回应用程序等)。但前者仅在从后台唤醒的情况下发布。请注意,在这种情况下,viewWillAppear 不会被调用(乍一看应该是这样)。

You need to use UIApplicationWillEnterForegroundNotification instead of UIApplicationDidBecomeActiveNotification.

The latter is posted every time your app becomes active (initial launch, back to app after call/sms interruption, etc.). But the former is posted only in case of wake up from background. Note that in this case viewWillAppear is not called (as it should seems to be at the first sight).

美人如玉 2025-01-09 20:20:55

一种方法是使用一个标志,您可以在 didFinishLaunching 中设置该标志,因为每次启动仅执行一次。

One way to do it would be with a flag, that you can set up in didFinishLaunching, since that is only executed once per launch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文