【问题标题】:Cocoa App. (MAC) is very slow [duplicate]可可应用程序。 (MAC)非常慢[重复]
【发布时间】:2012-10-19 06:45:01
【问题描述】:

我正在使用基于计时器的应用程序,我在主运行循环中连续运行 3 个计时器,以便在任何 UI 更新的情况下计时器将连续运行。但过了一段时间(大约 1 小时),我的应用程序挂起,而不是按钮等被按下。响应至少需要 10-15 秒。

我想知道 MAC cocoa 应用程序滞后的原因是什么。我正在开发一个基于计时器的应用程序。所以我需要多个计时器来运行。当没用时,我也无效。但它仍然没有帮助我。

请为 mac 的任何性能调试提供指导,以便我可以检查我的代码在哪里创建问题等?

代码:

NSRunLoop *runloop = [NSRunLoop currentRunLoop]; 
updateServerTimeTimer = [NSTimer timerWithTimeInterval:1.0 
                                                target:self
                                              selector:@selector(updateServerTime)
                                              userInfo:nil 
                                               repeats:YES]; 
[runloop addTimer:updateServerTimeTimer forMode:NSRunLoopCommonModes];

【问题讨论】:

  • NSRunLoop *runloop = [NSRunLoop currentRunLoop]; updateServerTimeTimer = [NSTimer timerWithTimeInterval:1.0 目标:self 选择器:@selector(updateServerTime) userInfo:nil 重复:YES]; [runloop addTimer:updateServerTimeTimer forMode:NSRunLoopCommonModes];
  • 我认为你的问题是 NSRunLoopCommonModes。为什么要使用 NSRunLoopCommonModes?

标签: macos cocoa nstimer nsviewcontroller nstabview


【解决方案1】:

您的问题是NSRunLoopCommonModes .使用scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 方法,它将在NSDefaultRunLoopMode 中安排NSTimer。如果 UI 正在更新,您的应用程序稍后会收到其他事件。看看this 的帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2022-09-27
    • 1970-01-01
    • 2012-10-12
    • 2020-04-09
    • 2017-04-27
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多