IOS Runloop Detailed

Source: Internet
Author: User

Before writing this article, I do not know what runloop is, if the translation from the literal meaning should be a cycle of running, suspicion may be related to the deadlock, but the details of the deadlock is how, I just remember that there is this statement, but also found a knowledge that they do not understand.

First knowledge of Runloop

I took a look at @sunnnyxx about Runloop on the internet. Learn about Runloop related knowledge, also go to the network to see a variety of runloop on the story.

Our general procedure is to execute a thread, a straight line, a starting point, and Runloop is always on the thread above the circle, has been running the circle, unless cut off otherwise has been running. The analogy on the internet is very good, the straight line is like a flash, the circle is like the OS, running until you shut down the computer.

In our learning iOS life cycle there will be the destruction process, but the screen seems to have been able to receive a variety of instructions, feel like runloop effect, as if these are not related to the top layer Uikit, iOS architecture is the core OS, I analysis should be Apple package, We just don't see the source.

Why to use Runloop

Back to the beginning of the question, why use runloop, generally we do not need to start the thread of Runloop, unless it is necessary to detect an event in a separate thread for a long time, like the video mentioned in the similar speech function, see a runloop specifically responsible for listening to the thread. Depends on the demand.

Cfrunloopsource

Source is Runloop's data source abstract class, similar to iOS protocol

Runloop defines the source of two version

SOURCE0: Handle app internal events, the app is responsible for managing (triggering), such as Uievent,cfsocket

Source1: Managed by Runloop and kernel, Mach port drives such as Cfmach, Cfmessage

Cfrunloopobserver

Report internal Runloop changes to the current state caanimation

Runloopobserver and Autorelease Pool

Uikit the Autorelease object generated in this loop by runloopobserver the pop and push of the autoreleasepool between two sleep runloop. (as if there were no release issues in Swift)

Cfrunloopmode

Runloop at the same time only and must change mode in a specific mode, you need to pause the current loop and then restart the new loop

Nsdefalutrunloopmode default state, idle state

Uitrackingrunloopmode Sliding ScrollView

Uiinitializationrunloopmode Private, when the app starts

Nsrunloopcommonmodes default includes first and second above

Uitrackingrunloopmode and Nstimer

By default Nstimer is added to Nsdefalutrunloopmode if you want Nstimer to be affected by component or animation effects to Nsrunloopcommonmodes (OC code is as follows:)

[[NSRunLoop currentRunLoop]addTimer:timer...forMode:NSRunLoopCommonModes];

Swift Version Code:

     NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

  

runloopmode Toggle

nsdefaultrunloopmode->uitrackingrunloopmode->nsdefal Utrunloopmode

runloop suspend and Wake

Mach_port port for Wakeup

Call Mach_msg Listen wake-up port, before wake up, The system kernel suspends this thread, stays on Mach_msg_trap

, and after another thread (or a thread in another process) sends the MSG to the kernel, the trap state wakes up and Runloop continues to work

afnetworking create Runloop

Create a resident service thread Very good method

[[Nsthread CurrentThread] setname:@ "afnetworking"]; Nsrunloop *runloop = [Nsrunloop currentrunloop]; [Runloop addport: [Nsmachport Port] formode:nsdefalutrunloopmode]//has been alive [Runloop run];

Swift Version Code       var loop = NSRunLoop.currentRunLoop()
 loop.addPort(NSMachPort(), forMode: NSDefaultRunLoopMode) loop.run()

A new idea of tableview delay loading pictures

IOS Runloop Detailed

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.