iOS NSNotification - 退出暂停时如何安全地忽略

发布于 2024-12-13 11:03:35 字数 571 浏览 5 评论 0原文

有时,当我正在开发的应用程序退出挂起模式时,我需要确保它不会响应 iOS 在应用程序挂起时为其保存的通知。在哪里执行此取消操作的正确位置可以确保我的响应代码不会运行?

我希望将removeObserver 放在applicationWillEnterForeground 中可以解决问题,但我在NSNotification 文档中找不到任何有关此内容的信息。

编辑: 让我添加场景的详细信息:

我正在开发的应用程序检查未来 7 天范围内的日历事件的更改。它两次刷新其数据(重新查询 eventStore): 1. 当 iPhone 日历发出有关更改的 NSNotification 时。 2. 应用程序第一次能够在新的一天进行,因为 7 天范围已更改。

我面临的设计挑战是两种情况同时成立的情况(应用程序从暂停状态返回,这是新的一天,并且日历事件在应用程序暂停时被修改)。在这种情况下,我只想重新查询事件存储一次。我的想法是,在 applicationWillEnterForeground 中,如果这是当天第一次启动,则告诉应用程序忽略或删除任何通知。 我的印象是,我对 NSNotifications 在这种情况下如何工作的假设可能是错误的。任何指导都会很棒!

Sometimes, when the app I'm working on comes out of suspended mode, I need to make sure it does not respond to the notifications iOS has saved for it while the app was suspended. Where's the right place to perform this cancellation where I can be sure my response code won't run?

I'm hoping that putting removeObserver is applicationWillEnterForeground would do the trick, but I couldn't find anything about this in the NSNotification doc.

Edit:
Let me add the details of the scenario:

The app I'm working on checks for changes to calendar events for the range of the next 7 days. It refreshes it's data (requeries eventStore) at two times:
1. When an NSNotification comes from the iPhone calendar about changes.
2. The first time the app is able to on a new day, because the 7 day range has changed.

The design challenge I'm having is the case where both scenarios are true at the same time (the app returns from being suspended and it's a new day, and calendar events were modified while the app was suspended ). I only want to requery the event store one time in that case. My thought is to, in applicationWillEnterForeground, on the case that it's the first launch of the day, tell the app to ignore or drop any notifications.
I'm getting the impression my assumptions about how NSNotifications would work in this case might be off. Any guidance would be great!

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

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

发布评论

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

评论(2

赢得她心 2024-12-20 11:03:35

为什么不在 -applicationDidEnterBackground: 中从通知中心移除观察者?

然后你可以在-applicationWillEnterForeground:中再次开始观察。

Why not remove the observer from the notification centre in -applicationDidEnterBackground:?

Then you can start observing again in -applicationWillEnterForeground:.

一抹淡然 2024-12-20 11:03:35

当你被挂起时,会出现什么 NSNotifications?您正在运行后台任务吗?

我真的想不出不响应 NSNotification 是个好主意的情况。它们实际上是与其他消息一样的消息,只是具有不同的路由机制。

UILocalNotification 将是一个不同的问题。

What NSNotifications come in while you’re suspended? Are you running a background task?

I can’t really think of a case where not responding to an NSNotification would be a good idea. They are effectively messages like any other, just with a different routing mechanism.

UILocalNotification would be a different issue.

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