我的 mac os x 应用程序没有响应,即使我使用 nsoperations 并将 maxConcurrentOperationCount 设置为 3
我长期以来一直在研究堆栈溢出,但从未真正有机会提出我的第一个问题,所以这里是:
我正在开发一个 mac os x 应用程序并使用 nsoperations 来保持应用程序响应, 我还将 maxConcurrentOperationCount 设置为 3,但是应用程序在执行工作时仍然有些无响应,如果它试图移动窗口,我可以看到它开始滞后并且行为不稳定,
有人可以提供任何线索或解决方案吗? (不,不是询问示例代码;)
i have been looking at stack overflow for long time but never really got a chance to make my first question, so here it is:
i am developing a mac os x app and using nsoperations to keep the app responsive,
i also set maxConcurrentOperationCount to 3, however the app is still somewhat unresponsive while doing its work, if it ry to move the window around i can see it starts to lag and behave erratically
can someone provide any clue or pointer to solution ?
(no, not asking sample code ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,应用程序可能无响应的原因有很多:
您直接阻塞了主事件循环或用事件淹没了它
您在主线程上进行复杂的绘图操作
您的应用程序使用了太多内存,导致系统分页。无论您有 10 个线程还是 1 个线程,只要您开始分页,您的性能就会急剧下降
主线程和后台线程/队列之间存在锁争用
Instruments 提供了一系列用于分析 CPU 使用情况的工具。我要做的第一件事是弄清楚主线程是否使用了大量 CPU(如果是,那么是为了什么?)或者它是否因等待锁等而被阻塞。
There are a number of reasons why an app might be unresponsive in such a situation:
you are straight up blocking the main event loop or flooding it with events
you have complex drawing operations on the main thread
your app is using so much memory that it is causing the system to page. Doesn't really matter if you have 10 threads or 1 thread, as soon as you start paging, your performance goes down the tubes
you have lock contention between the main thread and the background thread(s)/queue(s)
Instruments offers a series of tools for profiling CPU usage. The first thing I'd do is to figure it if the main thread is using a lot of CPU (and, if so, for what?) or if it is blocked waiting on locks or the like.
如果应用程序变得无响应,则说明您在代码中的某个位置阻塞了主线程,请使用活动监视器或工具(推荐)进行采样,以找出代码中的位置。
If the app becomes unresponsive you are blocking the main thread somewhere in your code, take a sample using activity monitor or instruments ( recommended ) to find out where in your code.
仅使用 NSOperations 不会使应用程序响应。响应能力的关键是不阻塞主线程。如果您的应用程序滞后,那是(通常 - 请参阅@bbum 的答案)因为您正在执行某些操作或某些操作阻塞了主线程。
找出什么的方法是使用Instruments。使用 Time Profiler 工具,然后查看主线程上正在运行的内容。将这些事情变得更小,将它们转移到操作中,延迟执行它们,或者它们的某种组合。如果你需要重构,就去做吧。
一种可能性是您正在 主队列。不要这样做——它们将在主线程上串行运行(无论 maxOperationCount 是多少)。创建队列并使用您创建的队列。
Just using NSOperations won't make the app responsive. The key to responsiveness is to not block the main thread. If your app is laggy, it's (usually—see @bbum's answer) because you're doing something or things that are blocking the main thread.
The way to find out what is to use Instruments. Use the Time Profiler instrument, and then look at what is running on the main thread. Make those things smaller, move them to operations, delayed-perform them, or some combination thereof. If you need to refactor, do it.
One possibility is that you are running your operations on the main queue. Don't do that—they will run serially (regardless of
maxOperationCount
) on the main thread. Create a queue and use the queue you created.