你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios多線程 -- NSOperation 簡介

ios多線程 -- NSOperation 簡介

編輯:IOS開發綜合

NSOperation的作?:配合使用NSOperation和NSOperationQueue也能實現多線程編程
NSOperation和NSOperationQueue實現多線程的具體步驟:
1)先將需要執行的操作封裝到一個NSOperation對象中
2)然後將NSOperation對象添加到NSOperationQueue中
3)系統會?動將NSOperationQueue中的NSOperation取出來
4)將取出的NSOperation封裝的操作放到?條新線程中執?

NSOperation是個抽象類,並不具備封裝操作的能力,必須使?它的子類
使用NSOperation?類的方式有3種:
1)NSInvocationOperation
2)NSBlockOperation
3)自定義子類繼承NSOperation,實現內部相應的?法

NSInvocationOperation 開啟任務方法

//用 NSInvocationOperation 創建線程
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];
//在當前線程中同步執行
[invocation start];

- (void)download:(NSString *)url{
    NSLog(@"%@ --- %@", [NSThread currentThread], url);
}

同步

將創建的任務添加到隊列中,異步執行

- (void)invocationOperation{
    //用 NSInvocationOperation 創建線程
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];  
    //創建全局隊列(實現異步並發執行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //從對列中取出任務執行(異步)
    [queue addOperation:invocation];

}

- (void)download:(NSString *)url{
    NSLog(@"%@ --- %@", [NSThread currentThread], url);
}

異步
注意:操作對象默認在主線程中執行,只有添加到隊列中才會開啟新的線程。即默認情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會異步執行操作<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPk5TQmxvY2tPcGVyYXRpb24gv6rG9MjOzvG1xLe9t6g8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;"> NSBlockOperation 添加任務 - (void)blockOperation2{ //創建任務 NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"下載圖片1 --- %@", [NSThread currentThread]); }]; [operation1 start]; }

這裡寫圖片描述

注意:只要NSBlockOperation封裝的操作數 > 1,就會異步執行操作

- (void)blockOperation2{
    //注意:只要NSBlockOperation封裝的操作數 > 1,就會異步執行操作
    //創建任務
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下載圖片1 --- %@", [NSThread currentThread]);
    }];

    [operation1 addExecutionBlock:^{
        NSLog(@"下載圖片2 --- %@", [NSThread currentThread]);
    }];

    [operation1 addExecutionBlock:^{
        NSLog(@"下載圖片3 --- %@", [NSThread currentThread]);
    }];

    [operation1 start];
}

這裡寫圖片描述

NSOperationQueue

NSOperationQueue的作?:NSOperation可以調?start?法來執?任務,但默認是同步執行的
如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動異步執行NSOperation中的操作
添加操作到NSOperationQueue中,自動執行操作,自動開啟線程
獲取 NSOperationQueue 隊列:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];//並發隊列
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主隊列

添加任務有兩個方法:

- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block; 

實例代碼

- (void)blockOperation{
    //創建全局隊列(實現異步並發執行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];//並發隊列
    //NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主隊列

    //創建任務
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下載圖片1 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下載圖片2 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下載圖片3 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下載圖片4 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下載圖片5 --- %@", [NSThread currentThread]);
    }];

    //將任務添加到隊列中
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    [queue addOperation:operation4];
    [queue addOperation:operation5];
}

這裡寫圖片描述

- (void)queueAddBlock{
    //創建全局隊列(實現異步並發執行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    //直接向隊列中添加任務
    [queue addOperationWithBlock:^{
         NSLog(@"下載圖片1 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下載圖片2 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下載圖片3 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下載圖片4 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下載圖片5 --- %@", [NSThread currentThread]);
    }];
}

這裡寫圖片描述

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved