[iOS Learning note]runloop RunMode method Research

Source: Internet
Author: User

////VIEWCONTROLLER.M//ThreadTest////Created by Skyko on 16/5/31.//copyright©2016 year Helios. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic, strong) Nsthread*thread; @property (nonatomic, assign) BOOL isAlive; @property (nonatomic, strong) Nstimer*timer;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; NSLog (@"Viewdidload"); Self.isalive=YES; Self.thread=[[Nsthread alloc] initwithtarget:self selector: @selector (workthread:) Object: nil]; [Self.thread setName:@"Mycustomthread"];    [Self.thread start]; //add a task to a thread after delaying 3sdispatch_time_t time = Dispatch_time (Dispatch_time_now,3*nsec_per_sec); Dispatch_after (Time, Dispatch_get_main_queue (),^{[Self performselector: @selector (handlesomething) OnThread:self.thread W    Ithobject:nil Waituntildone:no]; });}- (void) Workthread: (ID) Data {NSLog (@"Workthread"); Nsrunloop*runloop =[Nsrunloop Currentrunloop]; [Runloop Addport:[nsmachport Port] formode:nsdefaultrunloopmode];//If you do not add this line, the while loop that follows the task before it is added will be executed for the reasons explained later         while(_isalive) {//The IsAlive is used to control the running cycle of the thread, and when the IsAlive is set to No, the thread exits and destroys the current task after it finishes executingNSLog (@"--->"); //tasks that perform Nsdefaultrunloopmode mode within the set time//If no task is temporarily blocked in the queue until a task enters the queue//The task executes and then loops//timeout will go into the next loop[Runloop runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]; NSLog (@"<---"); }}- (void) handlesomething {NSLog (@"handlesomething before Sleep"); [Nsthread sleepfortimeinterval:1.0f]; NSLog (@"handlesomething after sleep");}@end

Run Results Print:

 .- to- to  One: the:25.020threadtest[1940:76011] Viewdidload .- to- to  One: the:25.020threadtest[1940:76221] Workthread .- to- to  One: the:25.021threadtest[1940:76221]---> .- to- to  One: the:28.311threadtest[1940:76221] handlesomething before sleep .- to- to  One: the:29.315threadtest[1940:76221] handlesomething after sleep .- to- to  One: the:29.315threadtest[1940:76221] <--- .- to- to  One: the:29.315threadtest[1940:76221]--->

The original document describes:

Runs the loop once, blocking for input in the specified mode until a given date.

Loop once to block the input waiting for a given pattern until a given point in time

Parameters

Mode

The mode in which to run. Specify custom modes or use one of the modes listed in Run Loop modes.

Limitdate

The date until which to block.

Return Value

YES If the run loop ran and processed an input source or if the specified timeout value is reached; otherwise, NO If the run loop could not be started.

When Runloop is running and processing an input source or time-out returns Yes, it returns no if Runloop is not started

Discussion

If no input sources or timers is attached to the run loop, this method exits immediately and returns no; Otherwis E, it returns after either the first input source is processed or limitdate is reached. Manually removing all known input sources and timers from the run loop does not guarantee, that the run loop would exit Imme Diately. OS X may install and remove additional input sources as needed to process requests targeted at the receiver ' s thread. Those sources could therefore prevent the run loop from exiting.

If no input source or timer is added to Runloop, the method exits immediately and returns no (this is why the following code was added to the demo above), or the method returns after the first input source or timeout in the queue has been executed. Manually removing all input sources in the Runloop and the timer does not guarantee that runloop exits immediately. OS x systems may add or remove some additional input sources in the thread to handle the required tasks, so these input sources may prevent Runloop from exiting.

[Runloop Addport:[nsmachport Port] Formode:nsdefaultrunloopmode];


NOTE

A timer is not considered an input source and may fire multiple times while waiting for this method to return

The timer is not an input source and may be started multiple times while waiting for the method to return

Summarize:

[Runloop Runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture];
1. Perform the task of setting the mode within the set time 2. If there are no tasks in the queue that are blocked until a task enters queue 3. The method exits after the task execution completes 4. The method exits after a timeout

[iOS Learning note]runloop RunMode method Research

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.