ios開發多線程一:瞭解-NSOperation的基本使用

來源:互聯網
上載者:User

標籤:

 

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self blockOperation];}/** *    1:NSOperation封裝任務的兩種方式:NSInvocationOperation,NSBlockOperation,其中兩種方法若沒指定隊列,則預設是在主隊列裡去執行 2:住家任務:addExecutionBlock: 注意:如果一個操作中的任務數量大於1,那麼會開子線程並發執行任務  注意:不一定是子線程,有可能是主線程 */-(void)invocationOpeation{        //1.建立操作,封裝任務    /*     第一個參數:目標對象 self     第二個參數:調用方法的名稱     第三個參數:前面方法需要接受的參數 nil     */     NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];     NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];     NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];        //2.啟動|執行操作     [op1 start];     [op2 start];     [op3 start];}-(void)blockOperation{    //1.建立操作    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"1----%@",[NSThread currentThread]);    }];        NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"2----%@",[NSThread currentThread]);    }];        NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"3----%@",[NSThread currentThread]);    }];        //追加任務    //注意:如果一個操作中的任務數量大於1,那麼會開子線程並發執行任務    //注意:不一定是子線程,有可能是主線程    [op3 addExecutionBlock:^{        NSLog(@"4---%@",[NSThread currentThread]);    }];        [op3 addExecutionBlock:^{        NSLog(@"5---%@",[NSThread currentThread]);    }];        [op3 addExecutionBlock:^{        NSLog(@"6---%@",[NSThread currentThread]);    }];        //2.啟動    [op1 start];    [op2 start];    [op3 start];}-(void)download1{    NSLog(@"%s----%@",__func__,[NSThread currentThread]);}-(void)download2{    NSLog(@"%s----%@",__func__,[NSThread currentThread]);}-(void)download3{    NSLog(@"%s----%@",__func__,[NSThread currentThread]);}@end

###2.NSOperation

- 2.1 NSOperation基本使用

 

(1)相關概念

 

    01 NSOperation是對GCD的封裝

    02 兩個核心概念【隊列+操作】

 

(2)基本使用

 

    01 NSOperation本身是抽象類別,只能只有它的子類

    02 三個子類分別是:NSBlockOperation、NSInvocationOperation以及自訂繼承自NSOperation的類

    03 NSOperation和NSOperationQueue結合使用實現多線程並發

 

(3)相關代碼

```objc

//  01 NSInvocationOperation

    //1.封裝操作

    /*

     第一個參數:目標對象

     第二個參數:該操作要調用的方法,最多接受一個參數

     第三個參數:調用方法傳遞的參數,如果方法不接受參數,那麼該值傳nil

     */

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]

                                        initWithTarget:self selector:@selector(run) object:nil];

 

    //2.啟動操作

    [operation start];

-------------------------------------------------

    //  02 NSBlockOperation

    //1.封裝操作

    /*

     NSBlockOperation提供了一個類方法,在該類方法中封裝操作

     */

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

        //在主線程中執行

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

    }];

 

    //2.追加操作,追加的操作在子線程中執行

    [operation addExecutionBlock:^{

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

    }];

 

    [operation addExecutionBlock:^{

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

    }];

 

    //3.啟動執行操作

    [operation start];

 

----------------------------------------------

// 03 自訂NSOperation

    //如何封裝操作?

    //自訂的NSOperation,通過重寫內部的main方法實現封裝操作

    -(void)main

    {

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

    }

 

    //如何使用?

    //1.執行個體化一個自訂動作對象

    XMGOperation *op = [[XMGOperation alloc]init];

 

    //2.執行操作

    [op start];

```

 

ios開發多線程一:瞭解-NSOperation的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.