NSOperation(Queue) 教程

1.NSOperation

NSInvocationOperation

- (void)invocationOperation{
    NSInvocationOperation *invOpt = [[NSInvocationOperation alloc] initWithTarget:self
                                                                         selector:@selector(run)
                                                                           object:nil];
    [invOpt start];
}
//2016-08-09 18:47:04.620 NSOperation[48338:755464] run-----{number = 1, name = main}

NSBlockOperation

- (void)blockOperation1{
    NSBlockOperation *blockOpt = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载1------%@", [NSThread currentThread]);
    }];
    [blockOpt start];
}
//2016-08-09 18:51:04.787 NSOperation[48546:758737] 下载1------{number = 1, name = main}

一个NSOperation执行start操作,默认在主线程执行

- (void)blockOperation2{
    NSBlockOperation *blockOpt = [NSBlockOperation blockOperationWithBlock:^{
        // 在主线程
        NSLog(@"下载1------%@", [NSThread currentThread]);
    }];
    
    // 添加额外的任务(在子线程执行)
    [blockOpt addExecutionBlock:^{
        NSLog(@"下载2------%@", [NSThread currentThread]);
    }];

    [blockOpt addExecutionBlock:^{
        NSLog(@"下载3------%@", [NSThread currentThread]);
    }];
    
    [blockOpt addExecutionBlock:^{
        NSLog(@"下载4------%@", [NSThread currentThread]);
    }];
    
    [blockOpt addExecutionBlock:^{
        NSLog(@"下载5------%@", [NSThread currentThread]);
    }];
    
    [blockOpt addExecutionBlock:^{
        NSLog(@"下载6------%@", [NSThread currentThread]);
    }];
    
    [blockOpt start];
}

//2016-08-09 18:57:23.753 NSOperation[48889:764202] 下载1------{number = 1, name = main}
//2016-08-09 18:57:23.754 NSOperation[48889:764202] 下载5------{number = 1, name = main}
//2016-08-09 18:57:23.754 NSOperation[48889:764202] 下载6------{number = 1, name = main}
//2016-08-09 18:57:23.753 NSOperation[48889:764231] 下载3------{number = 4, name = (null)}
//2016-08-09 18:57:23.753 NSOperation[48889:764238] 下载2------{number = 2, name = (null)}
//2016-08-09 18:57:23.753 NSOperation[48889:764232] 下载4------{number = 3, name = (null)}

NSBlockOperation封装的block操作数 >1 就会开启子线程异步并发执行

2.NSOperationQueue 与 NSOperation

NSInvocationOperation

- (void)run{
    NSLog(@"run---%@",[NSThread currentThread]);
}
- (void)operationQueueWithInvocationOperation{
    NSInvocationOperation *invOpe = [[NSInvocationOperation alloc] initWithTarget:self
                                                                         selector:@selector(run)
                                                                           object:nil];
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    [que addOperation:invOpe];
}
//2016-08-10 11:28:20.993 NSOperationQueue[53590:830749] run---{number = 2, name = (null)}
- (void)operationQueueWithBlockOperation{
    NSBlockOperation *blockOpe = [[NSBlockOperation alloc] init];
    [blockOpe addExecutionBlock:^{
        [self run];
    }];
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    [que addOperation:blockOpe];
}

//2016-08-10 11:34:22.216 NSOperationQueue[53877:834875] run---{number = 2, name = (null)}

把1个NSOperation添加到1个NSOperationQueue中,NSOperation封装的操作就会开启一个子线程 在子线程中执行

3.NSOperationQueue 与 自定义NSOperation

ZYXOperation.h

#import 

@interface ZYXOperation : NSOperation
@end

ZYXOperation.m

#import "ZYXOperation.h"

@implementation ZYXOperation

/**
 * 需要执行的任务
 */
- (void)main
{
    for (NSInteger i = 0; i<5; i++) {
        NSLog(@"download1 -%zd-- %@", i, [NSThread currentThread]);
    }
    if (self.isCancelled) {return;}
    
    for (NSInteger i = 0; i<5; i++) {
        NSLog(@"download2 -%zd-- %@", i, [NSThread currentThread]);
    }
    if (self.isCancelled) {return;}
    
    for (NSInteger i = 0; i<5; i++) {
        NSLog(@"download3 -%zd-- %@", i, [NSThread currentThread]);
    }
    if (self.isCancelled) {return;}
}

@end
- (void)operationQueueWithZYXOperation{
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    [que addOperation:[[ZYXOperation alloc] init]];
}

//2016-08-10 11:41:27.199 NSOperationQueue[54291:841376] download1 -0-- {number = 2, name = (null)}
//2016-08-10 11:41:27.200 NSOperationQueue[54291:841376] download1 -1-- {number = 2, name = (null)}
//2016-08-10 11:41:27.200 NSOperationQueue[54291:841376] download1 -2-- {number = 2, name = (null)}
//2016-08-10 11:41:27.202 NSOperationQueue[54291:841376] download1 -3-- {number = 2, name = (null)}
//2016-08-10 11:41:27.202 NSOperationQueue[54291:841376] download1 -4-- {number = 2, name = (null)}
//2016-08-10 11:41:27.203 NSOperationQueue[54291:841376] download2 -0-- {number = 2, name = (null)}
//2016-08-10 11:41:27.204 NSOperationQueue[54291:841376] download2 -1-- {number = 2, name = (null)}
//2016-08-10 11:41:27.204 NSOperationQueue[54291:841376] download2 -2-- {number = 2, name = (null)}
//2016-08-10 11:41:27.205 NSOperationQueue[54291:841376] download2 -3-- {number = 2, name = (null)}
//2016-08-10 11:41:27.206 NSOperationQueue[54291:841376] download2 -4-- {number = 2, name = (null)}
//2016-08-10 11:41:27.206 NSOperationQueue[54291:841376] download3 -0-- {number = 2, name = (null)}
//2016-08-10 11:41:27.207 NSOperationQueue[54291:841376] download3 -1-- {number = 2, name = (null)}
//2016-08-10 11:41:27.207 NSOperationQueue[54291:841376] download3 -2-- {number = 2, name = (null)}
//2016-08-10 11:41:27.208 NSOperationQueue[54291:841376] download3 -3-- {number = 2, name = (null)}
//2016-08-10 11:41:27.208 NSOperationQueue[54291:841376] download3 -4-- {number = 2, name = (null)}
- (void)operationQueue{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperationWithBlock:^{
        NSLog(@"download1 --- %@", [NSThread currentThread]);
    }];
}

//2016-08-10 11:53:15.578 NSOperationQueue[54846:848301] download1 --- {number = 2, name = (null)}

4.NSOperationQueue 设置

/** 队列 */
@property (nonatomic, strong) NSOperationQueue *queue;

- (void)handleOperationQueue{
    if (self.queue == nil) {
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:[[ZYXOperation alloc] init]];
        self.queue = queue;
    }
    
    if (self.queue.isSuspended) {
        // 恢复队列,继续执行
        self.queue.suspended = NO;
    } else {
        // 暂停(挂起)队列,暂停执行
        self.queue.suspended = YES;
    }
    
    // 取消掉队列中的操作
    // [self.queue cancelAllOperations];
}

//queue.maxConcurrentOperationCount = 2;
  queue.maxConcurrentOperationCount = 1; // 就变成了串行队列

5.NSOperation之间设置依赖

-(void)setOperationDependency{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download1----%@", [NSThread  currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download2----%@", [NSThread  currentThread]);
    }];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download3----%@", [NSThread  currentThread]);
    }];

    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"download4----%@", [NSThread  currentThread]);
        }
    }];
    
    NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download5----%@", [NSThread  currentThread]);
    }];
    op5.completionBlock = ^{
        NSLog(@"op5执行完毕---%@", [NSThread currentThread]);
    };
    
    // 设置依赖
    [op3 addDependency:op1];
    [op3 addDependency:op2];
    [op3 addDependency:op4];
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
    [queue addOperation:op4];
    [queue addOperation:op5];
}


// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905725] download1----{number = 2, name = (null)}
// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905978] download5----{number = 5, name = (null)}
// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905724] download2----{number = 3, name = (null)}
// 2016-08-10 13:42:50.491 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.493 NSOperation-操作依赖[60227:905978] op5执行完毕---{number = 5, name = (null)}
// 2016-08-10 13:42:50.493 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.493 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.494 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.494 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.550 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.550 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.551 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.551 NSOperation-操作依赖[60227:905721] download4----{number = 4, name = (null)}
// 2016-08-10 13:42:50.551 NSOperation-操作依赖[60227:905721] download3----{number = 4, name = (null)}

6.NSOperationQueue 之间的通信

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/**
 * 线程之间的通信
 */
- (void)threadCommunication{
    [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
        // 图片的网络路径
       NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        // 加载图片
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 生成图片
        UIImage *image = [UIImage imageWithData:data];
        // 回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
    }];
}
- (void)operationDependencyThreadCommunication{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    __block UIImage *image1 = nil;
    NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        image1 = [UIImage imageWithData:data];
    }];
    
    __block UIImage *image2 = nil;
    NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        image2 = [UIImage imageWithData:data];
    }];
    
    // 合成图片
    NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
        // 开启新的图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(100, 100));
        
        // 绘制图片
        [image1 drawInRect:CGRectMake(0, 0, 50, 100)];
        image1 = nil;
        
        [image2 drawInRect:CGRectMake(50, 0, 50, 100)];
        image2 = nil;
        
        // 取得上下文中的图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        // 结束上下文
        UIGraphicsEndImageContext();
        
        // 回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
    }];
    
    [combine addDependency:download1];
    [combine addDependency:download2];
    
    [queue addOperation:download1];
    [queue addOperation:download2];
    [queue addOperation:combine];
}

你可能感兴趣的:(NSOperation(Queue) 教程)