在 iOS 4 (iPhone) 上使用 NSInitationOperation (NSOperation) 和 NSOperationQueue 会导致速度显着下降

发布于 2024-09-19 07:53:49 字数 616 浏览 6 评论 0原文

我有一个很长的操作 O,它是通过 NSInitationOperation 调用的,它本身是通过将其添加到 NSOperationQueue 中来调度的,以便它异步运行。在我的应用程序中,在两种不同的情况下调用了冗长的操作 O。

在情况 A 中,操作 O 是由于在某个视图中点击某个小部件而调用的。一旦点击小部件,操作 O 就会运行一段时间(感谢 UIActivityIndi​​cator,我可以看到这一点),但它不会减慢或阻止 UI,因此我可以点击其他小部件并执行其他 UI 操作当操作 O 正在运行时。

在情况 B 中,操作 O 作为接收本地通知的结果而被调用,在应用程序委托的 didReceiveLocalNotification 方法中。在这种情况下,在调用操作 O 后立即执行的 UI 操作(尽管仍在 didReceiveLocalNotification 方法中)会显着减慢,基本上慢得像爬行一样,几乎就像操作 O 接管了 CPU 一样。

为什么会这样?在情况 B 中调用操作 O 的正确方法是什么,以便它确实以较低的优先级在后台并发运行,而使 didReceiveLocalNotification 方法中的其余代码以正常速度运行?

注意:操作 O 会破坏本地通知(删除现有通知或安排新通知)和日历(查询事件存储以更好地安排本地通知)。

I have a lengthy operation O that is called through a NSInvocationOperation, itself scheduled by adding it to a NSOperationQueue so that it runs asynchronously. That lengthy operation O is invoked in two different cases in my app.

In case A, operation O is invoked as a result of tapping some widget in some view. As soon as the widget is tapped, operation O runs for a while (I can see that thanks to a UIActivityIndicator), but it does not slow down or block the UI, thus I am able to tap on other widgets and perform other UI operations while operation O is running.

In case B, operation O is invoked as a result of receiving a local notification, in the app delegate's didReceiveLocalNotification method. In this case, UI operations performed right after invoking operation O, while still in the didReceiveLocalNotification method, are considerably slowed down, basically to a crawl, almost as if operation O had taken over the CPU.

Why is that, and what would be the right way to invoke operation O in case B, so that it indeed runs concurrently in the background with a lower priority, leaving the rest of the code in didReceiveLocalNotification method to run at a normal speed?

NOTE: Operation O mucks around with local notifications (removing existing ones or scheduling new ones) and calendars (querying the event store to better schedule local notifications).

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

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

发布评论

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

评论(1

仙女山的月亮 2024-09-26 07:53:49

您是否尝试过降低线程优先级?

这仅适用于 iOS 4,但您可以在 NSInitationOperation 上调用方法 setThreadPriority

Have you tried lowering the thread priority?

This is will only work on iOS 4, but you can call the method setThreadPriority on the NSInvocationOperation.

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