理解 NSRunLoop [英] Understanding NSRunLoop

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

问题描述

谁能解释一下什么是NSRunLoop?所以我知道 NSRunLoop 是一个与 NSThread 相关的东西,对吗?所以假设我创建了一个像

这样的线程

NSThread* th=[[NSThread alloc] initWithTarget:self selector:@selector(someMethod) object:nil];[第一个开始];-(void) someMethod{NSLog(@"操作");}

那么在这个线程完成他的工作之后呢?为什么使用 RunLoops 或在哪里使用?从 Apple 文档中我读到了一些东西,但对我来说不清楚,所以请尽可能简单地解释

解决方案

运行循环是一种抽象,它(除其他外)提供一种机制来处理系统输入源(套接字、端口、文件、键盘、鼠标、计时器、等).

每个 NSThread 都有自己的运行循环,可以通过 currentRunLoop 方法访问.

通常,您不需要直接访问运行循环,尽管有一些(网络)组件可能允许您指定它们将用于 I/O 处理的运行循环.

给定线程的运行循环将等待,直到它的一个或多个输入源有一些数据或事件,然后触发适当的输入处理程序来处理每个就绪"的输入源.

这样做之后,它将返回到它的循环,处理来自各种来源的输入,如果没有工作要做,则休眠".

这是一个非常高级的描述(尽量避免太多细节).

编辑

试图解决评论.我把它打碎了.

<块引用>
  • 这意味着我只能访问/运行以在线程内运行循环对吗?

确实如此.NSRunLoop 不是线程安全的,只能从运行循环的线程的上下文中访问.

<块引用>
  • 有没有什么简单的例子来向运行循环添加事件?

如果你想监控一个端口,你只需将该端口添加到 run loop,然后 run loop 会监视该端口的活动.

- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode

您也可以使用

显式添加计时器

- (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode

<块引用>

  • 什么意味着它将返回到它的循环?

运行循环将在每次迭代中处理所有就绪事件(根据其模式).您需要查看文档以了解运行模式,因为这有点超出一般答案的范围.

<块引用>
  • 当我启动线程时运行循环是否处于非活动状态?

在大多数应用程序中,主运行循环会自动运行.但是,您负责启动运行循环并响应您旋转的线程的传入事件.

<块引用>
  • 是否可以在线程外向线程运行循环添加一些事件?

我不确定你在这里的意思.您不会向运行循环添加事件.您添加输入源和计时器源(来自拥有运行循环的线程).然后运行循环监视它们的活动.当然,您可以提供来自其他线程和进程的数据输入,但输入将由运行循环处理,该运行循环正在运行运行循环的线程上监视这些数据源.

<块引用>
  • 这是否意味着有时我可以使用run loop来阻塞线程一段时间

确实如此.实际上,运行循环将停留"在事件处理程序中,直到该事件处理程序返回为止.您可以在任何应用程序中轻松地看到这一点.为任何休眠的 IO 操作(例如,按下按钮)安装处理程序.您将阻塞主运行循环(和整个 UI),直到该方法完成.

这同样适用于任何运行循环.

我建议您阅读以下有关运行循环的文档:

https://developer.apple.com/documentation/foundation/nsrunloop

以及它们在线程中的使用方式:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW1

Can anyone explain for what is NSRunLoop? so as I know NSRunLoop is a something connected with NSThread right? So assume I create a Thread like

NSThread* th=[[NSThread alloc] initWithTarget:self selector:@selector(someMethod) object:nil];
[th start];

-(void) someMethod
{
    NSLog(@"operation");
}

so after this Thread finishes his working right? why use RunLoops or where to use ? from Apple docs I have read something but its not clear for me, so please explain as simple as it possible

解决方案

A run loop is an abstraction that (among other things) provides a mechanism to handle system input sources (sockets, ports, files, keyboard, mouse, timers, etc).

Each NSThread has its own run loop, which can be accessed via the currentRunLoop method.

In general, you do not need to access the run loop directly, though there are some (networking) components that may allow you to specify which run loop they will use for I/O processing.

A run loop for a given thread will wait until one or more of its input sources has some data or event, then fire the appropriate input handler(s) to process each input source that is "ready.".

After doing so, it will then return to its loop, processing input from various sources, and "sleeping" if there is no work to do.

That's a pretty high level description (trying to avoid too many details).

EDIT

An attempt to address the comment. I broke it into pieces.

  • it means that i can only access/run to run loop inside the thread right?

Indeed. NSRunLoop is not thread safe, and should only be accessed from the context of the thread that is running the loop.

  • is there any simple example how to add event to run loop?

If you want to monitor a port, you would just add that port to the run loop, and then the run loop would watch that port for activity.

- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode

You can also add a timer explicitly with

- (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode

  • what means it will then return to its loop?

The run loop will process all ready events each iteration (according to its mode). You will need to look at the documentation to discover about run modes, as that's a bit beyond the scope of a general answer.

  • is run loop inactive when i start the thread?

In most applications, the main run loop will run automatically. However, you are responsible for starting the run loop and responding to incoming events for threads you spin.

  • is it possible to add some events to Thread run loop outside the thread?

I am not sure what you mean here. You don't add events to the run loop. You add input sources and timer sources (from the thread that owns the run loop). The run loop then watches them for activity. You can, of course, provide data input from other threads and processes, but input will be processed by the run loop that is monitoring those sources on the thread that is running the run loop.

  • does it mean that sometimes i can use run loop to block thread for a time

Indeed. In fact, a run loop will "stay" in an event handler until that event handler has returned. You can see this in any app simply enough. Install a handler for any IO action (e.g., button press) that sleeps. You will block the main run loop (and the whole UI) until that method completes.

The same applies to any run loop.

I suggest you read the following documentation on run loops:

https://developer.apple.com/documentation/foundation/nsrunloop

and how they are used within threads:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW1

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

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