iOS 多线程 NSOperation、NSOperationQ

作者: 圆脸黑猫警长 | 来源:发表于2016-10-11 16:42 被阅读119次

开发过程中为了保证界面操作的流畅,很多操作都需要放在子线程去执行,以免阻塞主线程。这时就需要多线程的知识了。

平常使用多线程的地方很多第三方库已经进行了封装,比如AFNetworking、SDWebImage等,使用者不需去关心异步的操作。

多线程的使用通常有3种方式:
1.NSThread
2.GCD
3.NSOperation(对GCD的封装)

本文主要讲解NSOperation的使用

NSOperation是抽象类,一般不直接使用,可以使用其子类:
NSInvocationOperation, NSBlockOperation,或者继承NSOperation重写其特定的一些方法
1.NSInvocationOperation
- (void)viewDidLoad
{
    [super viewDidLoad];
    //创建NSInvocationOperation对象
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"123"];
   //开始执行
    [op start];
}

- (void)test:(NSString *)str
{
    //do sth
    NSLog(@"%@",[NSThread currentThread]);
}

某次运行结果:
<NSThread: 0x7f9058d07cc0>{number = 1, name = main}

注意:number表示线程编号,1为主线程。由运行结果可以看出直接使用仍然是在主线程中执行的。

2.NSBlockOperation
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{     
    NSLog(@"第一个:%@",[NSThread currentThread]);
}];

[op addExecutionBlock:^{
    NSLog(@"第二个:%@",[NSThread currentThread]);
}];

[op addExecutionBlock:^{
    NSLog(@"第三个:%@",[NSThread currentThread]);
}];

[op start];

某次运行结果:
第一个:<NSThread: 0x7fbf13c027a0>{number = 1, name = main}
第三个:<NSThread: 0x7fbf13c119b0>{number = 3, name = (null)}
第二个:<NSThread: 0x7fbf13d11040>{number = 2, name = (null)}

注意:由运行结果可知 第一个一定是主线程中运行的,addExecutionBlock中的会另起线程并发执行,执行顺序不定。

3.继承NSOperation

重写 - (void)mian 方法。此方法中的操作在此线程中是同步执行的。

@implementation MyOperation
- (void)main
{
    //do sth
    NSLog(@"MyOperation main : %@",[NSThread currentThread]);
}

//使用方法
MyOperation *op = [[MyOperation alloc] init];
[op start];

某次运行结果:
MyOperation main : <NSThread: 0x7fb85a705060>{number = 1, name = main}

若要自己定义异步执行就需要重写- (void)start方法,此时会执行start方法不会去执行main方法,需要注意的是需要我们自己去管理operation的状态。

//这两个属性在父类中为readonly,子类需要复写
@property (nonatomic, assign) BOOL finished;
@property (nonatomic, assign) BOOL executing;

- (void)start
{
    if ([self isCancelled])
    {
        _finished = YES;
        return;
    }
    else
    {
        _executing = YES;
    
        //开始异步
    
        //完成异步
      
        if ([self isCancelled])
        {
            //每做完一些异步操作就需要判断是否取消
            _executing = NO;
            _finished = YES;
            return;
        }
        else
        {
            //do sth
            _executing = NO;
            _finished = YES;
        }
    }
}

//需要手动发送KVO通知
- (void)setFinished:(BOOL)finished
{
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}

//需要手动发送KVO通知
- (void)setExecuting:(BOOL)executing
{
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}
4.NSOperationQueue

前3个若直接使用都会在主线程中执行,这是我们更所不期望看到的,此时需要借助NSOperationQueue对象,此对象会把需要执行的Operation加入到一个队列中,然后并发执行队列中的Operation。

//创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//创建操作
MyOperation *op = [[MyOperation alloc] init];
//将操作加入队列即可
[queue addOperation:op];

** 注意:
1.此处不要调用start 方法。
2.加入队列后会自动并发执行。全部都开启线程,不在主线程中执行。
3.NSOperationQueue有属性maxConcurrentOperationCount可以设置最大并发执行数,若设置为1,则效果为FIFO(先进先出)。
**

相关文章

网友评论

    本文标题:iOS 多线程 NSOperation、NSOperationQ

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