image

编辑人: 桃花下浅酌

calendar2025-06-01

message7

visits983

ObjectC 将一个函数在主线程执行的4种方法是什么?

  • GCD方法,通过向主线程队列发送一个block块,使block里的方法可以在主线程中执行。 > dispatch_async(dispatch_get_main_queue(), ^{
        需要执行的方法
    });
  • NSOperation 方法 > NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];  主队列
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        需要执行的方法
    }];
    [mainQueue addOperation:operation];
  • NSThread 方法 > [self performSelector:@selector(method) onThread:[NSThread mainThread] withObject:nil waitUntilDone:YES modes:nil];
    [self performSelectorOnMainThread:@selector(method) withObject:nil waitUntilDone:YES];
    [[NSThread mainThread] performSelector:@selector(method) withObject:nil];
  • RunLoop方法 > [[NSRunLoop mainRunLoop] performSelector:@selector(method) withObject:nil];

喵呜刷题:让学习像火箭一样快速,快来微信扫码,体验免费刷题服务,开启你的学习加速器!

创作类型:
原创

本文链接:ObjectC 将一个函数在主线程执行的4种方法是什么?

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。
分享文章
share