NS调用操作问题

发布于 2024-10-17 05:31:55 字数 696 浏览 4 评论 0原文

我的 Cocoa Mac 应用程序中有一个方法,可以遍历应用程序的 PubSub 客户端中的所有订阅源,并将所有条目标记为已读。

逻辑看起来像这样......

NSArray *feeds = [[PSClient applicationClient]feeds];

for(PSFeed *feed in feeds)
{
    for(PSEntry *entry in [feed entries])
    {
        entry.read = NO;
    }
}

如果有大量未读提要,则可能需要一段时间才能循环浏览所有提要(相对而言)。我希望用户能够做其他事情,例如滚动浏览文章,而其他事情在后台进行。因此,我有一个 NSInitationOperation 设置来调用该方法,就像这样...

NSInvocationOperation *opr = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(markAllReadOperation) object:nil];
[que addOperation:opr];
[opr release];

循环运行时应用程序仍然会经历严重的速度减慢。而且用户界面几乎没有响应。我是否遗漏了 NSInvocableOperation 的某些内容,或者我的逻辑本质上很慢?

I have a method in my Cocoa Mac application that goes through all subscribed feeds in my application's PubSub Client and marks all the entries as read.

The logic looks something like this...

NSArray *feeds = [[PSClient applicationClient]feeds];

for(PSFeed *feed in feeds)
{
    for(PSEntry *entry in [feed entries])
    {
        entry.read = NO;
    }
}

If there are a large number of unread feeds it can take a while to cycle through them all (relatively speaking). I would like the user to be able to do other things like scroll through articles while the other stuff is going on in the background. So I have an NSInvocationOperation setup to call the method, like so...

NSInvocationOperation *opr = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(markAllReadOperation) object:nil];
[que addOperation:opr];
[opr release];

The application still experiences severe slowdowns while the loop is running. And the UI is nearly unresponsive. Am I missing something with NSInvocationOperation, or is my logic just inherently slow?

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

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

发布评论

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

评论(2

甜宝宝 2024-10-24 05:31:55

这听起来像是……中央调度局的工作!

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [obj enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [obj setRead:NO];
        }];
    }];
});

当然,Grand Central Dispatch(和块)都是在 Mac OS X 10.6 中引入的,因此如果您需要支持旧版本的 OS X,则可能无法使用。

This sounds like a job for...Grand Central Dispatch!

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [obj enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [obj setRead:NO];
        }];
    }];
});

Of course, Grand Central Dispatch (and blocks) were both introduced in Mac OS X 10.6, so that may not be usable if you need to support older versions of OS X.

慕巷 2024-10-24 05:31:55

如果您的 UI 无响应,则听起来好像主线程上正在运行某些内容。您没有使用 mainQueue 是吗?

我认为最好的选择是使用 Instruments 来使用 Time Profiler 工具准确查看哪些代码导致速度减慢。

If your UI is unresponsive, then it sounds like something is running on the main thread. You're not using the mainQueue are you?

I think your best bet would be to use Instruments to see exactly what code is causing the slowdown using the Time Profiler instrument.

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