perfomSelector: withObject: afterDelay: 我可以要求低优先级吗?

发布于 2024-07-19 07:01:57 字数 92 浏览 12 评论 0原文

发送此消息时是否有某种方法可以指定我宁愿在所有待处理的 UI 事件(如果有)之后执行选择器? IE。 在事件队列中给我的 aterDelay 计时器一个较低的优先级。

Is there some way when sending this message to specify that I rather have my selector performed after all pending UI events, if any? Ie. give my aterDelay timer a lower priority in the event queue.

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

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

发布评论

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

评论(2

北方的巷 2024-07-26 07:01:57

实际上,performSelector:withObject:afterDelay: 不一定必须发生在主线程上; 这就是为什么有一个单独的方法performSelectorOnMainThread:withObject:waitUntilDone:performSelector:withObject:afterDelay: 的文档说

延迟后使用默认模式调用当前线程上接收器的方法。

如果您想在后台执行任务,您可以查看 +[NSThread detachNewThreadSelector:toTarget:withObject:],它将启动一个新线程来执行您的任务,并使 UI 保持响应。 使用单独的线程来执行长时间运行的任务通常是一个好主意,否则可能会锁定您的 UI,但它确实会增加复杂性。 如果您不熟悉线程,您最终可能会遇到没有任何意义的错误。

在上面的评论中,您提到您认为动画可能导致您的 UI 无响应。 如果您使用内置的动画支持(Core Animation 或 Cocoa 包装器之一),动画不应导致您的 UI 无响应。 UI 无响应通常意味着您的程序在让运行循环返回服务 UI 事件之前正在主线程上执行大量工作。

Actually, performSelector:withObject:afterDelay: doesn't necessarily have to occur on the main thread; that's why there's a separate method performSelectorOnMainThread:withObject:waitUntilDone:. The documentation for performSelector:withObject:afterDelay: says

Invokes a method of the receiver on the current thread using the default mode after a delay.

If you want to perform a task in the background, you might look into +[NSThread detachNewThreadSelector:toTarget:withObject:], which will launch a new thread to perform your task, and leave the UI responsive. Using a separate thread to execute a long-running task which might otherwise lock up your UI is generally a good idea, but it does add complexity. If you're not familiar with threading, you may end up with bugs that won't make any sense.

In a comment above, you mentioned that you think animation may be at fault for making your UI unresponsive. If you're using the built-in support for animation (Core Animation or one of the Cocoa wrappers), animation shouldn't make your UI unresponsive. An unresponsive UI generally means that your program is doing a lot of work on the main thread before letting the run loop get back to service UI events.

不…忘初心 2024-07-26 07:01:57

不直接。 如果您使用performSelector:withObject:afterDelay:,选择器将在主线程上执行,因此根据定义,它将在执行所有当前“待处理”UI 事件之后发生 -但这可能是在滚动或动画的中间,您可能会认为这是一个连续事件,但实际上是数百个单独的事件。

但是,您可以使用 performSelectorInBackground:withObject: 实现类似的效果,然后在被调用的方法中调用 [NSThread setThreadPriority:0.01]。 请小心 — 您正在打开一个后台线程,因此您无法执行任何 UI 调用。 但是,这将允许您在优先级低于主 UI 线程的后台线程上执行工作。 (记住要设置一个自动释放池,因为它在自己的线程中!)

Not directly. If you use performSelector:withObject:afterDelay: the selector is performed on the main thread, so by definition it will occur after all current "pending" UI events have been performed—but this might be in the middle of a scroll or animation, which you would probably consider to be one continuous event but is actually hundreds of separate ones.

However, you can achieve something similar using performSelectorInBackground:withObject:, and then calling [NSThread setThreadPriority:0.01] in the method being called. Be careful—you're opening a background thread, so you can't do any UI calls. However, this will allow you to do the work on a background thread with lower priority than the main UI thread. (Remember to set up an autorelease pool since it's in its own thread!)

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