0

Digging more than one day....Apple, Google, Slideshare and stackoverflow. But still not clear about NSRunLoop.

Every thread has a runloop by default.Application mainThread has mainRunLoop.
1. If MainRunLoop get input events is it creating new thread to execute it? Then another runLoop created? How then multiple thread and multiple runLoop work? Communicate?
2. If runLoop has no input event/task it sleeps.When a RunLoop ends?
3. Why i should care about runLoop?
4. Where i can use it?

Where i miss that i can't understand the life cycle?

Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
Sofeda
  • 1,361
  • 1
  • 13
  • 23

1 Answers1

2

Lets look on your`s list:

  1. Wrong. Threads do not have built-in runloop. They need to be created manually.

  2. Runloop doesn`t create another threads, its immediately executes an event. That is why at the main thread we can see locked interface - by heavy-load tasks in the main thread (UI in iPhone runs on the main thread). Runloops can communicate with each other with the help of mac ports.

  3. Runloop sleeps before the first event come, then wakes up and ends. Only exception - timer, but it will not runloop. Runloop need to start run every time after Event (in the loop). If you call the run, there is already a built-in loop.

  4. Can use to create some threads which must track or execute something periodically. For example, you can create a thread, when runloop for it and then other threads can execute it`s selectors through performSelector. This creates a background query processor, which does not require each time to create a new thread.

  • Thanks a lot.Please help me more.I need the clear flow of runLoop.When it creates, what it do, when ends.....where it lives....You already told a lot about it.But need the flow. – Sofeda Nov 18 '15 at 04:50
  • 2
    At the main thread runloop created when the application starts: UIApplication creates and configures it. Main thread runloop tasks: - Receive all the app events from the system (by adding relevant info sources to runloop) - UIApplication works with its own selectors and its delegates in this main runloop (the main thread). – NixSolutionsMobile Nov 18 '15 at 08:55
  • 1
    Main thread`s runloop running like everyone else. He sleeps and waits for messages from the sources. If, for example -> touch on the screen, he receives a message about it, wakes up and performs a single cycle. During this cycle, the event about touch transformed into an app events from UIResponder and eventually come to a particular method. At the beginning of the single cycle it creates autorelease pool, in the end of cycle - destroyed. – NixSolutionsMobile Nov 18 '15 at 08:57
  • Ok.Then what is the purpose of RunLoop in my app? i mean custom runloop.If the main thread and main runloop is handling touch event and other inputs so why should i care about it? Can't i implement my app's any feature without creating or using this NSRunLoop?? – Sofeda Nov 18 '15 at 09:11
  • Each thread (NSThread) object has its own NSRunLoop object (automatically created). Even main thread has its own NSRunLoop object (automatically created). There is exactly one run loop per thread. You neither create nor destroy a thread’s run loop.[link](https://izeeshan.wordpress.com/2014/07/22/nsrunloop-understanding/) – Sofeda Nov 18 '15 at 09:15
  • 1
    Last statement is wrong - new thread doesn`t create runloop automatically. Runloop usually not needed. It is needed in cases where there is a large set of tasks, which should be done in background. In this case creation a lot of threads is impractical. Better flow - single blank background thread. But if we does not create sleeping runloop for this thread -> thread will be destroyed immediately. – NixSolutionsMobile Nov 18 '15 at 09:21
  • 1
    It is certainly possible to create an infinite thread with a sleep function, but in this case there is a problem how to call useful functions on this thread. PerformSelectorOnThread will not work if the thread in which you need to call the selector has no runloop. – NixSolutionsMobile Nov 18 '15 at 09:23
  • So if i do a task in a thread or same in another in runLoop what difference will come? – Sofeda Nov 18 '15 at 09:33
  • 1
    Where is no big difference. The only difference is that in the first case, thread will end the flow after task and for new task will be created new thread. A second variance (with runloop) - thread will sleep and it will be ready for another run – NixSolutionsMobile Nov 18 '15 at 09:36
  • So NSRunLoop only give access of current thread runloop if it has and main thread runloop. I can inject my task their and also creating separate thread for my task.task will be only the input source related like timer,port etc.main runloop ends with app exit and custom runloop in a custom thread will live till the thread is live.am i right ? – Sofeda Nov 18 '15 at 09:52
  • 2
    Runloop not depend from the thread. situation is opposite. Not Thread keeps runloop but runloop prevent thead to close. So, app exits due to the fact, that runloop stops working (most likely unsubscribe it from all sources). Once runloop stops - application out of the loop of runloop and goes beyond the functions of UIApplicationMain (). It comes to an end, and finishes its work. – NixSolutionsMobile Nov 18 '15 at 11:32
  • 1
    Similarly, with the background thead - created inside runloop keeps this thread and keeps this function does not provide it to the end, until runloop stops. Runloop in fact - an endless loop with the condition. Performing hangs on this cycle and hanging it up performing various tasks until the cycle is not interrupted – NixSolutionsMobile Nov 18 '15 at 11:34
  • 1. NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(callsomethingelse) userInfo:nil repeats:YES]; NSThread *thread = [NSThread currentThread]; thread.name = @"My thread"; [self performSelector:@selector(start) onThread:thread withObject:nil waitUntilDone:NO]; NSRunLoop * runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:timer forMode:NSRunLoopCommonModes]; [runLoop run]; – Sofeda Nov 19 '15 at 06:15
  • 2.[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(callsomethingelse) userInfo:nil repeats:YES]; i am not understanding the action of both.Both are working fine when i write in textfield in UI by touch. – Sofeda Nov 19 '15 at 06:17
  • 3. i am using a loop for 10000 times in this @selector(callsomethingelse) ;From the mainThread and the Custom thread's runLoop.Both time my UI is freeze until the loop is completed. – Sofeda Nov 19 '15 at 09:04