I'm learning socket communication on iPhone, and its guide said something about CFRunloop
(it is a guide for CFNetwork
, can this be used on iOS?)
Where can I learn about runloop on iOS?API reference is not enough.
A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.
Role of a run loop On iOS, a run loop can be attached to a NSThread . Its role is to ensure that its NSThread is busy when there is work to do and at rest when there is none. The main thread automatically launches its run loop at the application launch.
The RunLoop. main uses several modes and switches to a non-default mode when user interaction occurs. However, RunLoop. main as a Combine scheduler only executes when the default mode is active. In other words, the mode is switched back to default when user interaction ends and the Combine closure executes.
Look at the "Run Loops" chapter of Apple's Threading Programming Guide. In brief:
One major pitfall is forgetting to run the run loop while waiting for a callback from a runloop source. This is sometimes a problem when you decide to busy-wait for something to happen on the main thread, but you're most likely to run into it when you create your own thread and register a runloop source with that runloop. You are responsible for establishing an autorelease pool and running the runloop if needed on non-main threads, since the application main function will not be there to do it for you.
You would do better to read Apple's Concurrency Programming Guide instead, which suggests alternatives to the runloop mechanism such as operation queues and dispatch sources. The "Replacing Run-Loop Code" section of the "Migrating Away from Threads" chapter suggests using dispatch sources instead of runloop sources to handle events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With