NSDefaultRunLoopMode与NSRunLoopCommonModes [英] NSDefaultRunLoopMode vs NSRunLoopCommonModes

查看:141
本文介绍了NSDefaultRunLoopMode与NSRunLoopCommonModes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的stackoverflow人,

Dear good people of stackoverflow,

就像上次一样,我特此提出一个我最近偶然发现的问题。我希望那里有人可以解释一下。

Just like the last time, I hereby bring up a question I recently stumble upon. I hope someone out there could shed some light on me.

每当我尝试下载后面的大文件时UIScrollView MPMapView 等等,一触摸iPhone屏幕,下载过程就会停止。值得庆幸的是,Jörn的精彩博客文章建议一个替代选项,使用 NSRunLoopCommonModes 进行连接。

Whenever I try to download a big file behind UIScrollView, MPMapView or something, the downloading process gets halted as soon as I touch iPhone screen. Thankfully, an awesome blog post by Jörn suggests an alternative option, using NSRunLoopCommonModes for connection.

这让我看看两种模式的细节,NSDefaultRunLoopMode和NSRunLoopCommonModes,但除了说

That gets me look into detail of the two modes, NSDefaultRunLoopMode and NSRunLoopCommonModes, but the apple document does not kindly explain, other than saying

NSDefaultRunLoopMode

NSDefaultRunLoopMode


处理NSConnection对象以外的输入源的模式。
这是最常用的运行循环模式。

The mode to deal with input sources other than NSConnection objects. This is the most commonly used run-loop mode.

NSRunLoopCommonModes

NSRunLoopCommonModes


使用此值添加到运行循环的对象作为模式由所有已声明为常用模式集成员的运行循环模式监视;请参阅描述CFRunLoopAddCommonMode了解详情。

Objects added to a run loop using this value as the mode are monitored by all run loop modes that have been declared as a member of the set of "common" modes; see the description of CFRunLoopAddCommonMode for details.

CFRunLoopAddCommonMode

CFRunLoopAddCommonMode


源,定时器和观察器被注册到一个或多个运行循环模式,并且仅在运行循环以其中一种模式运行时运行。常见模式是一组运行循环模式,您可以为其定义一组源,定时器和这些模式共享的观察者。例如,不是将源注册到每个特定的运行循环模式,而是可以将其注册到运行循环的公共伪模式,并且它将在每个运行循环模式下自动注册在共模模式中。同样,当模式是a时对于一组共同模式,已经注册到公共伪模式的任何源,定时器或观察者都被添加到新添加的共模。

Sources, timers, and observers get registered to one or more run loop modes and only run when the run loop is running in one of those modes. Common modes are a set of run loop modes for which you can define a set of sources, timers, and observers that are shared by these modes. Instead of registering a source, for example, to each specific run loop mode, you can register it once to the run loop’s common pseudo-mode and it will be automatically registered in each run loop mode in the common mode set. Likewise, when a mode is added to the set of common modes, any sources, timers, or observers already registered to the common pseudo-mode are added to the newly added common mode.

任何人都可以用人类语言解释这两个吗?

Can anyone please explain the two in human language?

推荐答案

运行循环是一种机制,允许系统唤醒休眠线程,以便它们可以管理异步事件。通常,当您运行一个线程(主线程除外)时,有一个选项可以在运行循环中启动线程。如果线程运行某种排序或长时间运行的操作而不与外部事件交互并且没有定时器,则不需要运行循环,但如果您的线程需要响应传入事件,则应将其附加到运行循环以便新事件到来时唤醒线程。这是 NSURLConnection 生成线程的情况,因为它们仅在传入事件(来自网络)时唤醒。

A run loop is a mechanism that allows the system to wake up sleeping threads so that they may manage asynchronous events. Normally when you run a thread (with the exception of the main thread) there is an option to start the thread in a run loop or not. If the thread runs some sort or long-running operation without interaction with external events and without timers, you do not need a run loop, but if your thread needs to respond to incoming events, it should be attached to a run loop in order to wake up the thread when new events arrive. This is the case of NSURLConnection generated threads, as they only wake on incoming events (from the network).

每个线程可以与多个运行循环相关联,或者可以与可以设置为在不同模式下工作的特定运行循环相关联。 运行循环模式是操作系统用来建立某些规则以便何时传递某些事件或收集它们以便稍后交付的约定。

Each thread can be associated to multiple run loops, or can be associated to a specific run loop that can be set to work in different modes. A "run loop mode" is a convention used by the OS to establish some rules for when to deliver certain events or collect them to be delivered later.

通常所有运行循环设置为默认模式,它建立管理输入事件的默认方式。例如:只要鼠标拖动(Mac OS)或触摸(在iOS上)事件发生,则此运行循环的模式将设置为事件跟踪;这意味着线程不会在新的网络事件中被唤醒,但是这些事件将在用户输入事件终止并且运行循环再次设置为默认模式时传递;显然,这是OS架构师为了优先考虑用户事件而不是后台事件而做出的选择。

Usually all run loops are set to the "default mode" which establishes a default way to manage input events. For example: as soon as a mouse-dragging (Mac OS) or touch (on iOS) event happens then the mode for this run loop is set to event tracking; this means that the thread will not be woken up on new network events but these events will be delivered later when the user input event terminates and the run loop set to default mode again; obviously this is a choice made by the OS architects to give priority to user events instead of background events.

如果您决定更改<$ c的运行循环模式$ c> NSURLConnection 线程,通过使用 scheduleInRunLoop:forModes:,然后你可以将线程分配给一个特殊的运行循环模式,而不是特定的默认运行循环。许多输入源(包括事件跟踪)使用称为 NSRunLoopCommonModes 的特殊伪模式。例如,将 NSURLConnection 的实例分配给共同模式意味着除了默认模式之外还将其事件与跟踪模式相关联。将线程与 NSRunLoopCommonModes 相关联的一个优点/缺点是线程不会被触摸事件阻止。

If you decide to change the run loop mode for your NSURLConnection thread, by using scheduleInRunLoop:forModes:, then you can assign the thread to a special run loop mode, rather than the specific default run loop. The special pseudo-mode called NSRunLoopCommonModes is used by many input sources including event tracking. For example assigning NSURLConnection's instance to the common mode means associates its events to "tracking mode" in addition to the "default mode". One advantage/disadvantage of associating threads with NSRunLoopCommonModes is that the thread will not be blocked by touch events.

新模式可以添加到常用模式,但这是一个非常低级别的操作。

New modes can be added to the common modes, but this is quite a low-level operation.

我想通过添加一些注释来结束:

I would like to close by adding a few notes:


  • 通常我们需要使用一组图像或从网络下载的带有表格视图的
    缩略图。我们可能认为
    从网络下载这些图像而桌面视图是
    滚动可以改善用户体验(因为我们可以在
    滚动时看到图像),但这不是有利的,因为
    滚动的流动性会受到很大影响。在此示例中, NSURLConnection 不应使用运行循环;最好使用 UIScrollView 委托方法来检测滚动何时终止,然后更新表并从网络下载新项
    ;

  • Typically we need to use a set of images or thumbnails downloaded from the network with a table view. We may think that downloading these images from the network while the table view is scrolling could improve the user experience (since we could see the images while scrolling), but this is not advantageous since the fluidity of scrolling can suffer greatly. In this example with NSURLConnection a run loop should not be used; it would be better to use the UIScrollView delegate methods to detect when scrolling is terminated and then update the table and download new items from the network;

您可以考虑使用GCD,这将帮助您屏蔽代码
与运行循环管理问题。在上面的示例中,您可以考虑将您的网络请求添加到自定义串行队列。

You may consider using GCD which will help you to "shield" your code from run loop management issues. In the example above, you may consider adding your network requests to a custom serial queue.

这篇关于NSDefaultRunLoopMode与NSRunLoopCommonModes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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