The mode of Nsrunloop in iOS

Source: Internet
Author: User
I. Nsrunloop

In cocoa, a run loop (Nsrunloop) object within each thread (Nsthread) object is used to loop through the input events, including two classes, one from the input sources asynchronous event, and one from the timer Sources synchronization events;
The run loop generates notifications when processing input events, by adding run-loop observers to threads through the core foundation to listen for specific events to do additional processing when a listener event occurs.

Each run loop runs in a different mode, and a run loop mode is a collection that contains several input event sources that it listens on, timers, and the run loop observers that needs to be notified when the event occurs. The run loop running in one mode only handles the input source events, timer events, and observers that are included in the run loop mode.
The predefined patterns in cocoa are: default mode
Definition: Nsdefaultrunloopmode (COCOA) Kcfrunloopdefaultmode (Core Foundation)
Description: In the default mode, almost all input sources (except Nsconnection) are included, and this pattern should normally be used. Connection mode
Definition: Nsconnectionreplymode (COCOA)
Description: Handling Nsconnection Object related events, internal use of the system, the user will not use the basic. Modal mode
Definition: Nsmodalpanelrunloopmode (COCOA)
Description: Handles modal panels events. Event Tracking Mode
Definition: Uitrackingrunloopmode (IOS) nseventtrackingrunloopmode (cocoa)
Description: In this mode when dragging the loop or other user interface tracking loops, this mode restricts the processing of input events. For example, you will be in this mode when your fingers hold down uitableview drag. Common mode
Definition: Nsrunloopcommonmodes (COCOA) kcfrunloopcommonmodes (Core Foundation)
Description: This is a pseudo pattern, which is a collection of run loop mode, and adding the input source to this mode means that it can be handled in all modes contained in common modes. In the cocoa application, the common modes contains default Modes,modal modes,event tracking. You can use the Modes method to Cfrunloopaddcommonmode Add custom modes to the modes.

Gets the current thread's run loop mode

nsstring* Runloopmode = [[Nsrunloop currentrunloop] currentmode];

two. Nstimer, Nsurlconnection and Uitrackingrunloopmode

Nstimer and Nsurlconnection run under default mode so that when the user drags UITableView in Uitrackingrunloopmode mode, Nstimer cannot fire, The nsurlconnection data cannot be processed either.
Examples of Nstimer:
Start a 0.2s loop timer in a uitableviewcontroller, update a counter when the timer expires, and display it on the label.

-(void) viewdidload
{
    label =[[[uilabel Alloc]initwithframe:cgrectmake (M.,)]autorelease];
    [Self.view Addsubview:label];
    Count = 0;
    Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1
                                                      target:self selector
                                                    : @selector (incrementcounter :)
                                                    userinfo:nil
                                                     repeats:yes];
}

-(void) Incrementcounter: (Nstimer *) Thetimer
{
    count++;
    Label.text = [NSString stringwithformat:@ '%d ', Count];
}

Normally, you can see the number +1 displayed on each 0.2s,label, but when you drag or hold TableView, the number on the label is no longer updated, and when your finger leaves, the number on the label continues to update. When you drag UITableView, the current thread run loop is in Uieventtrackingrunloopmode mode, in which case the timer event is not processed, and the number on which the timer cannot fire,label cannot be updated.
Workaround, one way is to handle the timer event in another thread, add a timer to the nsoperation and schedule it in another thread, and a way to modify the run loop mode of the timer running. Add it to Uitrackingrunloopmode mode or Nsrunloopcommonmodes mode.
That
[[Nsrunloop Currentrunloop] Addtimer:timer Formode:uitrackingrunloopmode];
Or
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];

The other one is put in the nsthread.[OBJC]  View plain copy-  (void) viewdidload{       [super viewDidLoad];        nslog (@ "main thread  %@",  [nsthread currentthread]);       //Create and execute a new thread        nsthread *thread = [[ Nsthread alloc] initwithtarget:self selector: @selector (newthread)  object:nil];       [thread start];  }  -  (void) newthread{        @autoreleasepool {       //Adds a timer to the current Run loop Mode is the default nsdefaultrunloopmode       [nstimer scheduledtimerwithtimeinterval:2.0  target:self selector: @selector (timer_callback)  userInfo:nil repeats:YES];       //starts executing the run loop of the new thread, and if the Run loop,timer event is not started, it will not respond         [[nsrunloop currentrunloop] run];       }  }  -  (void) timer_ callback{       nslog (@ "timer %@",  [nsthread currentthread]);   }  

The same is true for nsurlconnection, see the description in Sdwebimage, and the implementation in SDWEBIMAGEDOWNLOADER.M code. Modify the Nsurlconnection run mode to use the Scheduleinrunloop:formode: method.

Nsurlrequest *request = [[Nsurlrequest alloc] Initwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata TIMEOUTINTERVAL:15];
 Nsurlconnection *connection = [[[Nsurlconnection alloc] initwithrequest:request delegate:self StartImmediately:NO] Autorelease];
[Connection Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsrunloopcommonmodes];
[Connection start];

Reference:
Threading Programming Guide–run Loops
Nsrunloop Class Reference
Nsurlconnection Class Reference
Nstimer Class Reference
Cfrunloop Wiki
Sdwebimage
Testbuttondown
Nstimerdoesntrunwhenmenuclicked

This article originates from Qing, undisturbed's blog, when reprinted, please indicate the source and the corresponding link.

This article permanent link: http://www.winddisk.com/2012/06/29/nstimer_run_loop_modes/

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.