Internal flow of Runloop in iOS development and some additional notes

Source: Internet
Author: User

The front has written some basic content about runloop, many people say Runloop, then runloop have what function?

Its basic role:

♦ Keep the program running continuously (Runloop is an ever-running dead loop)

♦ Handles various events in the app (such as touch events, timer events, selector events)

♦ Save CPU resources, improve the performance of the program (because we can control it when we are working with Runloop, we should rest when we are doing it)

We know that when the app is launched, it will automatically open a runloop on the main thread, and if there is no runloop, then our program will end directly after launch.

Let's take a look at the processing logic of Runloop:

1, notice Observer: will enter loop

2, notice Observer: will handle timer

3, notice Observer: will be processed SOURCE0 (note: event source here the author according to the function call stack, divided into Source0 (user-initiated, not port-based ports), Source1 (Port-based ports))

4, processing Source1 (about Source1 We can understand this: the event is a port-based event, so internal distribution of the timer event and the Source0 event)

5, if there is Source1 to jump to the 9th step (here we can see the runloop execution loop, only the timer and the source of the event will be fully processed before going into hibernation, otherwise it will loop until the processing is finished)

6, notify Observer: Thread is about to hibernate

7. Hibernate, wait for Wake up

8, notice Observer: Line Cheng Gang is awakened

9. Process the message received at wake-up, then jump back to the 2nd step

10, notice Observer: Will exit loop (when the time of Runloop or when the thread hangs out will exit)

I've seen these. We have some understanding of Runloop's internal processing logic, and let's look at a classic usage of runloop.

One of the main classic applications of Runloop is the resident thread, such as when we use a sub-thread, when the thread is finished, the thread will hang, and then the start method that we call this thread will fail. At this point we can use Runloop to create a resident thread that prevents the thread from dying.

Auto-free Pool issues

The last is about Runloop's automatic release pool, we know that Runloop will create an auto-release pool, its first creation is when the Runloop is started, it was last destroyed when Runloop was launched, but Runloop was not created at the time of destruction, So when the thread is going to hibernate, it will destroy the auto-release pool once and recreate a new one.

The theory of Runloop for the time being to write these first, with these understandings should help the reader to understand the runloop and use it better.

Internal flow of Runloop in iOS development and some additional notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.