参考 :
https://dev59.com/d2oy5IYBdhLWcg3wJKvR#14741253
像上面的链接所说,但似乎没有解释原因。
在我的代码中,以下内容将起作用:
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector: @selector(helloWorld) withObject:nil afterDelay:0.5];
});
但是,当我注释掉类似这样的内容时,(我确信它在主线程中运行!!)代码就不能正常工作:
// dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector: @selector(helloWorld) withObject:nil afterDelay: 0.5];
// });
请问有人能告诉我为什么? 而且,“self”永远不会释放/解除分配,我会一直保留它直到应用程序结束。
“不起作用”表示(没有崩溃),它不会跳转到“helloWorld”方法:
-(void) helloWorld {
NSLog(@"hello world"); // I set a break point here for debug , it wouldn't pause forever
}
我认为是运行循环引起了这个问题。就像这个链接所说的那样,但我需要更多的细节或更明确的解释。
dispatch_after()
呢?是否有特别的原因呢? - user557219performSelector
调用之前添加以下代码:NSLog(@"%@ Main thread", [NSThread isMainThread] ? @"ON" : @"NOT ON");
,以查看您是否正在主线程上运行。 - Abizern