IOS does not block the main thread and can stop the code from going down


Go straight to the dry goods!

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    _stop = NO;
while (_stop) {
          [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate 
          Mylog (@ "while loop...);
The code you need to execute
_stop = YES;

Problem generation scenario: I encountered this situation in the for loop. The for loop is running all the time. I want it to stop and use various delay statements (for example, sleep (2);) The main thread will be stuck,
I found that the main thread of the card was blocked because I was involved in sokcet sending.

Then I searched for a way to stop the code from going down without blocking the main thread. I really found one, and it’s really easy to use

Solution: nsrunloop is the method. At the beginning, stop outside the for loop is yes. Modify the stop value in the asynchronous delay, and then judge with while. If the stop value is true, enter the runloop pause code and go down. After 2 seconds, the stop value becomes false. Jump out of the while loop and continue to execute. Stop becomes true and stops next time

Post a link to that article:

thank!!! Make a record of things that are not commonly used but will be used