iOS NSOperation高级使用

//

//  ViewController.m

//  NSOperaiton 使用Demo

//

//  Created by 邹彦军 on 16/7/20.

//  Copyright © 2016 邹彦军. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()

@property(nonatomic, strong)NSOperationQueue *queue;

@property(nonatomic, strong)UIButton *testButton;


@end


@implementation ViewController


- (void)downloadImage:(id)obj {

    

    NSLog(@"\n当前线程:%@\n传递的对象:%@", [NSThread currentThread], obj);

}



- (void)viewDidLoad {

    [super viewDidLoad];


    [self.view addSubview:self.testButton];

    [self operationDemo9];

}



- (void)operationDemo1 {

    

    NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:@"invocation"];

    // start方法 会在当前线程(当前是主线程)执行 @selector方法

    [op start];

}


- (void)operationDemo2 {

    // 新队列

    NSOperationQueue *queue = [NSOperationQueue new];

    // 操作

    NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:@"invocation"];

    // 新队列添加任务 会异步执行 downloadImage: 方法

    [queue addOperation:op];

}


- (void)operationDemo3 { // 添加多个任务

    

    NSOperationQueue *queue = [NSOperationQueue new];

    for (int i = 0; i < 10; i++) {

        NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:@(i)];

        // 执行效果: 会开启多条线程,所有任务没有顺序执行,效果与GCD的并发队列&异步执行效果一样

        [queue addOperation:op];

    }

}


- (void)operationDemo4 { // 效果和operationDemo2一样

    

    NSOperationQueue *queue = [NSOperationQueue new];

    

    // 使用block定义操作,代码写在一起 方便管理、维护

    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{

        [self downloadImage:@"invocation"];

    }];

    [queue addOperation:op];

}


- (void)operationDemo5 { // 效果和operationDemo4一样,写法更简单

    

    NSOperationQueue *queue = [NSOperationQueue new];

    

    // 更简单的,直接添加Block

    [queue addOperationWithBlock:^{

        [self downloadImage:@"invodation"];

    }];

}


- (void)operationDemo6 { // 队列中可以添加任何 NSOperation的子类

    

    NSOperationQueue *queue = [NSOperationQueue new];

    

    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{

        [self downloadImage:@"invocation"];

    }];

    // 添加 NSBlockOperation操作

    [queue addOperation:op1];

    

    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:@"invocation"];

    // 添加 NSInvocationOperation操作

    [queue addOperation:op2];

    

}


- (void)operationDemo7 { // 线程间通讯

    

    NSOperationQueue *queue = [NSOperationQueue new];

    

    [queue addOperationWithBlock:^{ // 子线程下载任务

        

        [self downloadImage:@"invocation"];

        

        [[NSOperationQueue mainQueue]addOperationWithBlock:^{ // 回到主线程更细UI

            NSLog(@"回到主线程 更新UI");

        }];

    }];

}


- (NSOperationQueue *)queue{

    if(_queue == nil){

        _queue = [NSOperationQueue new];

    }

    return  _queue;

}


- (void)operationDemo8 { // 高级功能: 最大并发数

    // '最大并发数' 同时执行任务的数量   比如同时开启三个线程,执行三个任务 并发数就是三

    

    [self.queue setMaxConcurrentOperationCount:3]; // 如果不设置最大并发数 会同时开启很多线程执行操作


    for (int i = 0; i < 10; i++) {

        NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{

            [NSThread sleepForTimeInterval:1];//模拟网络慢

            [self downloadImage:@"invocation"];

        }];

        // 添加操作

        [self.queue addOperation:op];

    }

}


- (UIButton *)testButton {

    if (_testButton == nil) {

        _testButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];

        _testButton.backgroundColor = [UIColor orangeColor];

        [_testButton addTarget:self action:@selector(cancleAllOperation) forControlEvents:UIControlEventTouchUpInside];

    }

    return _testButton;

}


- (void)pauseAndResume {

    if (self.queue.operationCount == 0) {

        NSLog(@"当前队列没有操作!");

        return;

    }

    self.queue.suspended = !self.queue.isSuspended;

    if (self.queue.isSuspended) { //不能取消正在执行的操作

        NSLog(@"队列暂停,剩余操作数: %zd",self.queue.operationCount);

    }else{

        NSLog(@"队列继续,剩余操作数: %zd",self.queue.operationCount);

    }

}


- (void)cancleAllOperation {

    if (self.queue.operationCount == 0) {

        NSLog(@"当前队列没有操作!");

        return;

    }

    [self.queue cancelAllOperations]; //取消全部操作 同样不能取消正在执行的操作

    NSLog(@"取消全部操作:%zd",self.queue.operationCount);

}


- (void)operationDemo9 { //添加操作依赖

    

    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{

        NSLog(@"登录--%@",[NSThread currentThread]);

    }];

    

    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{

        NSLog(@"付费--%@",[NSThread currentThread]);

    }];

    

    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{

        NSLog(@"下载--%@",[NSThread currentThread]);

        [NSThread sleepForTimeInterval:3];

    }];

    

    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{

        NSLog(@"主线程通知用户--%@",[NSThread currentThread]);

    }];

    

    [op2 addDependency:op1];

    [op3 addDependency:op2];

    [op4 addDependency:op3];

    [self.queue addOperations:@[op1,op2,op3] waitUntilFinished:NO];

    [[NSOperationQueue mainQueue]addOperation:op4];


    NSLog(@"√");

}



@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值