Thursday, May 16, 2024
HomeiOS Developmentios - does `NSRunLoop::runUntilDate` block the principle thread?

ios – does `NSRunLoop::runUntilDate` block the principle thread?


I’m doing a little experiment based mostly on this reply: https://stackoverflow.com/a/17921058/767653

That is my minimal reproducible code:


- (void)viewDidLoad {
  [super viewDidLoad];
  
  __block BOOL accomplished = NO;
  [self doSomethingAsyncAndCompleteOnMainThread:^{
    completed = YES;
  }];
  
  whereas (!accomplished) {
    NSLog(@"Anticipate 1 sec");
    [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
  }
  
  NSLog(@"Accomplished");
}


- (void)doSomethingAsyncAndCompleteOnMainThread:(void(^)(void))completion {
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    completion();
  });
}

My understanding is that, this code has a impasse. As a result of the completion block is run on fundamental thread, so to ensure that the completion block to run, viewDidLoad should return. And to ensure that viewDidLoad to return, completion block have to be run to toggle the accomplished flag (therefore round wait).

Nonetheless, after I run it, I acquired this print out:

Anticipate 1 sec
Anticipate 1 sec
Anticipate 1 sec
Accomplished

Which suggests there isn’t any impasse on this code. The place I did perceive wrongly?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments