美文网首页iOS进阶之路iOS
iOS主线程操作的几种方法

iOS主线程操作的几种方法

作者: 蜡笔小强 | 来源:发表于2016-11-29 15:03 被阅读72次

    在实际开发中,经常需要异步处理数据,然后刷新UI只能在主线程,以下是几种主线程操作的方法,gcd方法是大家最熟悉的。

    1. GCD方法(最常见,使用简单方便,苹果封装)

    dispatch_async(dispatch_get_main_queue(), ^{

         //do something

    });

    2.NSOperation方法

    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];主队列

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

    //do something

    }];

    [mainQueue addOperation:operation];//不要忘记加这句

    3. NSThread方法

    [selfperformSelector:@selector(method) onThread:[NSThread mainThread] withObject:nilwaitUntilDone:YESmodes:nil];

    [selfperformSelectorOnMainThread:@selector(method) withObject:nilwaitUntilDone:YES];

    [[NSThread mainThread] performSelector:@selector(method) withObject:nil];

    4. RunLoop方法

    [[NSRunLoop mainRunLoop] performSelector:@selector(method) withObject:nil];

    相关文章

      网友评论

        本文标题:iOS主线程操作的几种方法

        本文链接:https://www.haomeiwen.com/subject/kmnapttx.html