了解NSRunLoop [英] Understanding NSRunLoop

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

问题描述

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

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

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

所以在这个Thread完成他的工作权限后?为什么使用 RunLoops 或在哪里使用?从Apple文档我已经读了一些东西,但它不清楚我,所以请解释尽可能简单

解决方案

是一个抽象(其中包括)提供处理系统输入源(套接字,端口,文件,键盘,鼠标,定时器等)的机制。



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



一般来说,你不需要直接访问运行循环,虽然有一些)组件,可以允许你指定它们将用于I / O处理的运行循环。



给定线程的运行循环将等待,直到它的一个或多个输入源有一些数据或事件,然后触发相应的输入处理程序来处理ready的每个输入源。



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



这是一个非常高级的描述



尝试处理评论。





  • 这意味着我只能访问/运行以在线程中运行循环
    对吗?


NSRunLoop不是线程安全的,并且只应该从正在运行循环的线程的上下文中访问。



  • 有什么简单的例子如何添加事件到运行循环?


监视端口,您只需将该端口添加到运行循环,然后运行循环将监视该端口的活动。

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

也可以用

显式地添加一个计时器

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





  • 这意味着它将返回到其循环?


运行循环将根据其模式处理所有就绪事件。您将需要查看文档以了解运行模式,因为这超出了一般答案的范围。


    $ b

在大多数应用程序中,主运行循环将自动运行。



  • 是可以在线程之外的线程运行循环中添加一些事件?


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


$ b $


  • 这意味着有时候我可以使用运行循环来阻止线程一次。

    确实。事实上,运行循环将停留在事件处理程序中,直到该事件处理程序返回。你可以看到这在任何应用程序足够简单。为任何睡眠的IO操作(例如按钮按下)安装处理程序。您将阻塞主运行循环(和整个UI),直到该方法完成。



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



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



    https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsrunloop_Class/Reference/ Reference.html



    以及如何在线程中使用它们:



    https://developer。 apple.com/library/mac/#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/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsrunloop_Class/Reference/Reference.html

    and how they are used within threads:

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

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

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