Objective-C: How does the code in the main thread interact with its runloop?

How does the code in the main thread and its runloop interact? For example, should all the code in the main thread work until it is idle before it gets into runloop? Or does runloop check its sources in the middle of code execution in the main thread? Is it possible for the runloop source to block the execution of the main thread code (since it runs on the same thread)?

I am trying to understand how the main thread code fits into the runloop image (or vice versa) in a great scheme of things.

What runloop looks like next to our code:

Main theme:

  • runloop runs at a certain interval
  • runloop is running, our code is running
  • our code runs, go to (1) (What if our code runs for so long that runloop cannot run?)
+4
source share
2 answers

You asked:

How does the code in the main thread and its runloop interact?

The main runloop is just a loop that runs in the main thread.

What if our code runs for so long that runloop cannot run?

If you block the main thread (i.e. you do something synchronously, which is very slow), then the runloop that called your code and is waiting for your return will not be able to continue the loop, and therefore it all depends on what (timers, sources, user interface, etc.) will be frozen.

, - , , - , , , , . , , , runloop .

, , runloop, -. , runloop "", . , runloop , (, , ..) ( , runloop, , runloop).

+6

Runloop . , / runloop, , runloop . / .

: , : runloop . runloop , runloop . "runloop is running running, " , "runloop , , ".

+1

Source: https://habr.com/ru/post/1539372/