iOS NSOperation



- (void)viewDidLoad {

    [super viewDidLoad];

   


    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(100, 100, 120, 100);

    button.backgroundColor = [UIColor blueColor];

    [button setTitle:@"NSOperation" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(clickOperation) forControlEvents:UIControlEventTouchUpInside];


    UIButton *blockButton = [UIButton buttonWithType:UIButtonTypeSystem];

    blockButton.frame = CGRectMake(100, 220, 120, 100);

    blockButton.backgroundColor = [UIColor blueColor];

    [blockButton setTitle:@"BlockOperation" forState:UIControlStateNormal];

    [blockButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

    [self.view addSubview:blockButton];

    [blockButton addTarget:self action:@selector(clickBlock) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *queueButton = [UIButton buttonWithType:UIButtonTypeSystem];

    queueButton.frame = CGRectMake(100, 340, 120, 100);

    queueButton.backgroundColor = [UIColor blueColor];

    [queueButton setTitle:@"QueueOperation" forState:UIControlStateNormal];

    [queueButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

    [self.view addSubview:queueButton];

    [queueButton addTarget:self action:@selector(clickQueue) forControlEvents:UIControlEventTouchUpInside];

    

    

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];

    backButton.frame = CGRectMake(50, 50, 50, 50);

    [backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    [self.view addSubview:backButton];

    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

}



- (void)clickOperation {

    //NSOperation 是一个抽象类,它不具备实际的开辟线程的作用,将NSOperation放在哪个线程中, 它就再哪个线程中执行回调方法

    //这是NSOperation的一个子类,开辟子线程 三个参数的具体作用和NSThread alloc中的三个参数功能相同

    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:@"nil"];

    //执行invocationOperation的回调方法

      [invocationOperation start];

}


-(void)test {

    

    NSLog(@"当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);

    

}



- (void)clickBlock {

     //NSOperation的第二个子类,实现方式是通过block

    NSBlockOperation *blockOpe = [NSBlockOperation blockOperationWithBlock:^{

        

        NSLog(@"当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);

    }];

    // 开始去执行

    [blockOpe start];

}



- (void)clickQueue {

    //operationQueue队列,将operation加入到队列中之后,operation会自动在子线程中执行,并且是并发执行。不用在手动调用start方法,系统会调用

     //operationQueue队列中,执行是无序的。

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    NSBlockOperation *ope1 = [NSBlockOperation blockOperationWithBlock:^{

        

        NSLog(@"。当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);

    }];

    NSBlockOperation *ope2 = [NSBlockOperation blockOperationWithBlock:^{

        

        NSLog(@"。。当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);

    }];

    NSBlockOperation *ope3 = [NSBlockOperation blockOperationWithBlock:^{

        

        NSLog(@"。。。当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);

    }];

    

    

    //当我们想要队列中的执行时是有序的,就得给operation添加依赖关系,添加完依赖关系之后,先执行的是参数的operation。再执行调用依赖方法的operation,例如 [ope1 addDependency:ope2] 就先执行 ope2再执行ope1

    

    [ope1 addDependency:ope2];

    [ope2 addDependency:ope3];

    

    [queue addOperation:ope1];

    [queue addOperation:ope2];

    [queue addOperation:ope3];

}



- (void)back {

    [self dismissViewControllerAnimated:YES completion:nil];

}



@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值