[IOS Study Notes] runloop runMode method survey, runlooprunmode
/// ViewController. m // ThreadTest /// Created by skyko on 16/5/31. // Copyright©2016 helios. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) NSThread * thread; @ property (nonatomic, assign) BOOL isAlive; @ property (nonatomic, strong) NSTimer * timer; @ end @ implementation ViewController-(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]; // after a delay of 3 s, add the task dispatch_time_t time = dispatch_time (DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC) to the thread; dispatch_after (time, interval (), ^ {[self timer mselec: @ selector (handleSomeThing) onThread: self. thread withObject: nil waitUntilDone: NO];}) ;}- (void) workThread :( id) data {NSLog (@ "workThread"); nsunloop * runloop = [nsunloop currentRunLoop]; [runloop addPort: [NSMachPort port] forMode: NSDefaultRunLoopMode]; // if this row is not added, the following while loop will be executed until the task is added, the reason is as follows: while (_ isAlive) {// isAlive is used to control the running cycle of this thread. When isAlive is set to NO, this thread exits after executing the current task and destroys NSLog (@ "---> "); // execute a task in NSDefaultRunLoopMode within the set time // if there are no tasks in the queue temporarily, the task will be blocked until a task enters the queue // After the task is completed, the loop // timeout will enter the next loop [runloop runMode: describeforedate: [NSDate distantFuture]; NSLog (@ "<---") ;}}-(void) handleSomeThing {NSLog (@ "handleSomeThing before sleep"); [NSThread sleepForTimeInterval: 1.0f]; NSLog (@ "handleSomeThing after sleep");} @ end
Run result printing:
2016-05-31 11:42:25.020 ThreadTest[1940:76011] viewDidLoad2016-05-31 11:42:25.020 ThreadTest[1940:76221] workThread2016-05-31 11:42:25.021 ThreadTest[1940:76221] --->2016-05-31 11:42:28.311 ThreadTest[1940:76221] handleSomeThing before sleep2016-05-31 11:42:29.315 ThreadTest[1940:76221] handleSomeThing after sleep2016-05-31 11:42:29.315 ThreadTest[1940:76221] <---2016-05-31 11:42:29.315 ThreadTest[1940:76221] --->
Introduction to the original article:
Runs the loop once, blocking for input in the specified mode until a given date.
// Execute the loop once to block the input waiting for the specified mode until the specified time point.
Parameters
Mode |
The mode in which to run. You may 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 was reached; otherwise, NO if the run loop cocould not be started.
// If runloop is running and processing an input source or timeout, YES is returned. Otherwise, NO is returned if runloop is not started.
Discussion
If no input sources or timers are attached to the run loop, this method exits immediately and returns NO; otherwise, it returns after either the first input source is processed orLimitDateIs reached. manually removing all known input sources and timers from the run loop does not guarantee that the run loop will exit immediately. OS X may install and remove additional input sources as needed to process requests targeted at the specified er's thread. those sources cocould therefore prevent the run loop from exiting.
If NO input source or timer is added to runloop, the method immediately exits and NO is returned (this is why the following code is added in the demo above ), otherwise, this method will return after the first input source or timeout in the execution queue. manually removing all input sources and timer In the runloop does not guarantee that the runloop will exit immediately. the OS X system may add or remove some additional input sources in the thread to process required tasks. Therefore, these input sources may prevent runloop 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
Timer is not an Input Source and may be started multiple times while waiting for this method to return.
Summary:
[runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
1. execute the task in the set mode within the set time. if no task is in the queue, it will be blocked until a task enters the queue. this method exits after the task is completed. this method exits after timeout