In IOS development, the question of nsunloop and NSTimer

Source: Internet
Author: User

In the Windows era, you must have some knowledge about SendMessage, PostMessage, and GetMessage. These are all message processing functions in windows. What is corresponding to in ios? In fact, they are just the content of the nsunloop. In ios, all messages will be added to the nsunloop, which can be divided into 'input source' and 'timer source'. in the loop, check whether an event is required, if necessary, call the corresponding function for processing. When using NSTimer, we may be exposed to the concept of runloop. The following is a simple example: Copy code 1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 // Do any additional setup after loading the view, typically from a nib. 5 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 1 6 target: self 7 selector: @ selector (printMessage :) 8 userInfo: nil 9 repeats: YES]; 10} copy the Code. If we scroll a scrollview on the page, we will find that there will be no output in the console before the scrollView stops rolling, as if the scrollview will After checking the relevant documents, we found that the runloop mode is a strange task. Runloop can be understood as a message Loop Mechanism in cocoa to process various message events. We do not need to manually create a runloop during development, because the framework creates a default runloop for us, we can get a corresponding runloop object under the current thread through [nsunloop currentRunloop, however, we need to pay attention to the notification methods of messages between different runloop. Next, in the above topic, when an NSTimer is enabled, a new event source is registered in the current runloop. When the scrollView is rolled, the current MainRunLoop is in UITrackingRunLoopMode. In this Mode, messages in NSDefaultRunLoopMode are not processed (because the RunLoop Mode is different ), to receive messages from other runloop while scrollView is rolling, we need to change the runloopmode between the two. 1 [[nsunloop currentRunLoop] addTimer: timer forMode: nsunloopcommonmodes]; simply put, NSTimer won't start a new process, just register it in Runloop, runloop checks the timer every time it loops to see if it can be triggered. When Runloop is in A mode and timer is registered in B mode, it cannot detect this timer. Therefore, you need to register NSTimer to A mode so that it can be detected. Speaking of this, this problem may also occur in the http asynchronous communication module, that is, when receiving image data from the server asynchronously and notifying the main thread to refresh the image in tableView, when the tableView scroll is not stopped or the user's fingers are stuck on the screen, the picture will never come out, and the runloop mode may be behind it.

Related Article

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.