NSOperation的使用

2017-12-01  本文已影响13人  攻克乃还_

一、NSOperation基本使用

1.1.NSInvocationOperation

    //1.封装操作
    /*
     第一个参数:目标对象
     第二个参数:该操作要调用的方法,最多接受一个参数
     第三个参数:调用方法传递的参数,如果方法不接受参数,那么该值传nil
     */
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self selector:@selector(run) object:nil];
    //2.启动操作
    [operation start];

1.2.NSBlockOperation

    //1.封装操作
    /*
     NSBlockOperation提供了一个类方法,在该类方法中封装操作
     */
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        //在主线程中执行
        NSLog(@"---download1--%@",[NSThread currentThread]);
    }];
    //2.追加操作,追加的操作在子线程中执行
    [operation addExecutionBlock:^{
        NSLog(@"---download2--%@",[NSThread currentThread]);
    }];
    //3.启动执行操作
    [operation start];

1.3.自定义NSOperation

    -(void)main {
        NSLog(@"--main--%@",[NSThread currentThread]);
    }

    //如何使用?
    //1.实例化一个自定义操作对象
    XMGOperation *op = [[XMGOperation alloc]init];
    //2.执行操作
    [op start];

二、 NSOperationQueue基本使用

GCD中的队列:
NSOperation中的两种队列:
- (void)invocation
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.封装操作
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
    //3.把封装好的操作添加到队列中
    [queue addOperation:op1];//[op1 start]
    [queue addOperation:op2];
}
- (void)block
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.封装操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2----%@",[NSThread currentThread]);
    }];
    [op2 addExecutionBlock:^{
        NSLog(@"3----%@",[NSThread currentThread]);
    }];
    [op2 addExecutionBlock:^{
        NSLog(@"4----%@",[NSThread currentThread]);
    }];
    //3.添加操作到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];

    //补充:简便方法
    [queue addOperationWithBlock:^{
        NSLog(@"5----%@",[NSThread currentThread]);
    }];
}
-(void)customOperation
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.封装操作
    //好处:1.信息隐蔽 2.代码复用
    SUNOperation *op1 = [[SUNOperation alloc]init];
    SUNOperation *op2 = [[SUNOperation alloc]init];
    //3.添加操作到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];
}

三、NSOperation其它操作

a.设置最大并发数
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.设置最大并发数
    queue.maxConcurrentOperationCount = 2;
b.暂停、恢复、取消
    if (self.queue.isSuspended) {
        self.queue.suspended = NO;
    }else {
        self.queue.suspended = YES;
    }
c.取消队列里面的所有操作

[self.queue cancelAllOperations];

// 自定义NSOperation取消操作
-(void)main
{
    //耗时操作1
    for (int i = 0; i<1000; i++) {
        NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);
    }
    if (self.isCancelled) {
        return;
    }
    //耗时操作2
    for (int i = 0; i<1000; i++) {
        NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);
    }
}

四、NSOperation线程间通信

    // 子线程下载图片
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperationWithBlock:^{
        NSLog(@"下载图片--%@",[NSThread currentThread]);
        // 主线程刷新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSLog(@"刷新UI---%@",[NSThread currentThread]);
        }];
    }];

五、设置操作依赖

- (void)download2
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.封装操作:下载图片1
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
    }];
    //3.封装操作:下载图片2
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
    }];
    //4.封装操作:合成图片
    NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
        // 回到主线程刷新UI
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            NSLog(@"刷新UI---%@",[NSThread currentThread]);
        }];
    }];
    //5.设置操作依赖
    [combine addDependency:op1];
    [combine addDependency:op2];
    //6.添加操作到队列中执行
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:combine];
    }
上一篇下一篇

猜你喜欢

热点阅读