Problems with Nsrunloop and Nstimer in iOS development

Source: Internet
Author: User

In the era of Windows, you certainly know something about Sendmessage,postmessage,getmessage, which is the message processing function in Windows, which corresponds to what is in iOS, is actually nsrunloop this thing. In iOS, all messages are added to the Nsrunloop, and are divided into ' input source ' and ' timer source ', and check in the loop if there is an event that needs to happen, and then call the corresponding function if necessary.

When we use Nstimer, we may be exposed to the concept of Runloop,

Here's a simple example.

-(void) viewdidload{    [Super Viewdidload];     // additional setup after loading the view, typically from a nib.    Nstimer * timer = [Nstimer scheduledtimerwithtimeinterval:1                                              target:self                                            selector: @selector ( Printmessage:)                                            userinfo:nil                                             repeats:yes];}

This time if we scroll a scrollview on the interface, then we will find that the console will not have any output until the scrolling is stopped, as if the ScrollView paused the timer while scrolling, and found after viewing the corresponding document. This is actually Runloop's mode is doing strange.
Runloop can be understood as a message loop mechanism under cocoa, used to handle various message events, we do not need to manually create a runloop when developing, because the framework creates a default runloop for us, through the [Nsrunloop Currentrunloop] We can get a corresponding Runloop object under the current thread, but we need to be aware of the way messages are notified between different runloop.

Then the above topic, when opening a nstimer is essentially registering a new event source in the current Runloop, and when ScrollView scrolls, the current mainrunloop is in Uitrackingrunloopmode mode, In this mode, Nsdefaultrunloopmode messages are not processed (because Runloop mode is not the same),

We need to change the runloopmode between the two if we want to scrollview the other runloop while scrolling.

1 [[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];

Simply said that Nstimer will not open a new process, just registered in the Runloop, runloop Each loop will detect the timer, see if it can be triggered. When Runloop is in a mode and the timer is registered in B mode, it is not possible to detect the timer, so it is necessary to register the Nstimer with a mode so that it can be detected.

In this case, in the HTTP asynchronous communication module, it is also possible to encounter such a problem, that is, in the asynchronous acquisition of image data to the server to notify the main thread to refresh the picture in TableView, when the TableView scroll is not stopped or the user's finger stays on the screen, the picture will not come out, Maybe it's the Runloop mode in the back.

Source: http://www.cnblogs.com/xwang/

Problems with Nsrunloop and Nstimer in iOS development

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.