Understanding NSRunLoop

Can anyone explain what NSRunLoop ? since I know that NSRunLoop is something related to NSThread correctly? So suppose I create Thread as

 NSThread* th=[[NSThread alloc] initWithTarget:self selector:@selector(someMethod) object:nil]; [th start]; -(void) someMethod { NSLog(@"operation"); } 

So, after this thread finishes its working right? Why use RunLoops or where to use? I read something from Apple docs, but this is not clear to me, so please explain as simple as possible

+87
ios objective-c cocoa-touch nsrunloop
Aug 23 2018-12-12T00:
source share
4 answers

Starting a loop is an abstraction that (among other things) provides a mechanism for processing input data sources (sockets, ports, files, keyboard, mouse, timers, etc.).

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

In general, you do not need to directly access the startup loop, although there are some (network) components that can allow you to specify which loop loop they will use to process I / O.

The start cycle for this stream will wait for one or more of its input sources to have some data or an event, then run the appropriate input handler to process each input source that is β€œready”.

After that, he will return to his cycle, process input from various sources and "sleep" if there is no work.

This is a pretty high level description (an attempt to avoid too many details).

EDIT

Trying to reply to a comment. I broke it into pieces.

  • this means that I can only access / run to start the loop inside the thread right?

Really. NSRunLoop is not thread safe and should only be accessible from the context of the thread that starts the loop.

  • Is there any simple example of how to add an event to a loop loop?

If you want to control the port, you simply add this port to the run loop, and then the run loop will watch this port for activity.

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

You can also add a timer using

 - (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode 
  • What does it mean that it will return to the cycle?

The start of the loop will process all ready-made events at each iteration (in accordance with its mode). You will need to look at the documentation to find out about the launch modes, as this is a bit beyond the general answer.

  • starts a loop inactive when a thread starts?

In most applications, the main startup loop starts automatically. However, you are responsible for starting the execution loop and responding to incoming events for the threads you rotate.

  • Is it possible to add some events to the thread cycle loop outside the thread?

I'm not sure what you mean here. You do not add events to the run loop. You add input sources and timer sources (from the stream to which the trigger cycle belongs). Then the cycle loop tracks their activity. Of course, you can provide input from other threads and processes, but the input will be processed by a run loop that controls these sources in the thread that starts the run loop.

  • Does this mean that sometimes I can use the run loop to block the thread for a while

Really. In fact, the loop will remain in the event handler until that event handler returns. You can see this in any application quite simply. Set a handler for any I / O action (for example, clicking a button) that will sleep. You will block the main run loop (and the entire user interface) until this method completes.

The same applies to any startup cycle.

I suggest you read the following documentation on startup loops:

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

and how they are used in threads:

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

+174
Aug 23 '12 at 13:00
source share

RunLoops is a kind of box where things just happen.

Usually in RunLoop you process some events and then return. Or return if it does not handle events before the timeout. You can say that it is like asynchronous NSURLConnections, processing data in the background without affecting your current loop, and at the same time, you need data synchronously. What can be done with RunLoop, which performs an asynchronous NSURLConnection connection and provides data during a call. You can use RunLoop as follows:

 NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1]; while (YourBoolFlag && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:loopUntil]) { loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1]; } 

In this RunLoop, it will work until you finish some other work and set YourBoolFlag to false .

Similarly, you can use them in streams.

Hope this helps you.

+7
Oct. 14 '14 at 9:30 a.m.
source share

Execution loops are what separate interactive applications from command line tools.

  • Command-line tools run with parameters, execute their command, and then exit.
  • Interactive applications wait for user input, respond, then resume waiting.

From here

They allow you to wait until the user clicks and answers accordingly, wait until you get the completion, and apply its results, wait until you get the timer and execute the function. If you do not have runloop, then you cannot listen / wait for user clicks, you cannot wait until a network call is made, you cannot wake up in x minutes.

Also from this comment :

Background threads do not have their own run cycles, but you can just add them. For example, AFNetworking 2.x did this. It was a tried and true technique for NSURLConnection or NSTimer in background threads, but we ourselves do not do this anymore, as the new APIs eliminate the need to do this. But it seems that the URLSession is executing, for example, a simple request by executing [see Left panel of the image] completion handlers in the main queue, and you can see that it has a run loop in the background thread

+2
04 Aug '17 at 20:25
source share

Execution loops are part of the fundamental flow-related infrastructure. A run loop is an event loop that you use to schedule work and coordinate the reception of incoming events. The goal of the execution loop is to keep your thread busy when there is work, and put your thread to sleep when it is not.

From here




The most important feature of CFRunLoop is CFRunLoopModes. CFRunLoop works with Run Loop Sources. Sources are recorded in the run loop for one or more modes, and the run loop itself is executed to run in this mode. When an event arrives at the source, it is processed by the run loop only if the source mode matches the current run loop mode.

From here

0
Apr 25 '18 at 1:50
source share



All Articles