为什么performSelector:onThread:不好? [英] Why performSelector:onThread: is not good?

查看:162
本文介绍了为什么performSelector:onThread:不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要在 Apple文档:


注意线程之间的偶然通信,你不应该使用 performSelector:onThread:withObject:waitUntilDone:方法来进行线程之间的时间关键或频繁通信。

Note: Although good for occasional communication between threads, you should not use the performSelector:onThread:withObject:waitUntilDone: method for time critical or frequent communication between threads.

为什么会这样?多少是频繁的?

Why is it so? How much is "frequent"?

我有一个应用程序有很多线程和很多东西发生在主线程(重JavaScript,频繁的Ajax查询WebKit )。在Yosemite,我开始遇到大量的问题,结合几个(说,10)文件下载( NSURLDownload s)加上WebKit GUI(需要运行在主线程) 。

I have an app with a lot of threads and a lot of stuff happening in main thread (heavy JavaScript, frequent Ajax queries in a WebKit). On Yosemite, I started to experience huge problems in combination of several (say, 10) file downloads (NSURLDownloads) plus the WebKit GUI (required to run in the main thread).

文件下载不能与由JavaScript大量加载的WebKit活长。在某些时候,所有网络请求开始返回超时(错误-1001),直到应用退出。

File downloads cannot "live" long with a WebKit heavily loaded by JavaScript. At some point, all network requests start returning timeout (error -1001) until the app quits.

我广泛使用 performSelector:onThread:withObject :waitUntilDone:,例如以通知UI关于下载进度。这可能每秒发生许多次。可能是问题吗?

And I widely use performSelector:onThread:withObject:waitUntilDone:, e.g. to notify UI about the download progress. This can happen many times per second. Can it be the problem?

不幸的是,我不允许显示整个源代码...

P.S. Unfortunately, I'm not allowed to show the whole source code...

推荐答案

奇怪的是,在Yosemite文档中,在 NSObject 的文档中没有这样的警告...

Strangely, on the Yosemite documentation, there is no such warning in NSObject's documentation...

虽然也许只有苹果才能给出真正的原因,怀疑它与性能有关。请在 NSObject 中调用的文档中记下此段落:

Though perhaps only Apple can give the real reason, I suspect it has to do with performance. Note this paragraph in the documentation for that call in NSObject:


在目标线程的运行循环上使用默认运行循环模式的消息,即与 NSRunLoopCommonModes 常量相关的模式。作为其正常运行循环处理的一部分,目标线程使消息出队(假设它以默认运行循环模式之一运行)并调用所需的方法。

This method queues the message on the run loop of the target thread using the default run loop modes—that is, the modes associated with the NSRunLoopCommonModes constant. As part of its normal run loop processing, the target thread dequeues the message (assuming it is running in one of the default run loop modes) and invokes the desired method.

换句话说,当你使用这个方法时,你在系统的不同线程上传递对象,在这个过程中可能有一些开销。系统只保证它将被目标线程直接执行 - 它不会在执行时 。如果目标线程已经在做某事,你的方法可能不会被调用一段时间。因此,最好向线程发送消息(即更新标志或某事),而不是立即运行,因为像线程一样,不能保证立即执行。

In other words, when you're using the method, you're passing objects around on the different threads on the system, and there is probably some overhead in this process. The system only guarantees that it will be executed directly by the target thread - it does not say when it will be executed. If the target thread is already doing something, your method may not be called for some time. Therefore, it is better for sending messages to threads (I.e to update a flag or something) rather than having something run immediately because, like all things with threads, there is no guarantee of immediate execution.

总之,警告可能有两个原因:

To sum up, the warning is likely there for two reasons:


  • 这样做有一些开销,因此不适合发送快速消息

  • 不能保证方法将被执行;这一切取决于CPU调度程序给出线程执行时间的时间,以及发生这种情况时线程正在做什么

这篇关于为什么performSelector:onThread:不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆