iOS的多線程技術主要有:pthread、NSThread、NSOperation、GCD。怎么選呢?
pthread是c函數,如果不涉及底層編碼,一般就不要用了
NSThread出來比較早,需要管理生命周期,沒有必要也不要用
NSOperation有文章說是對GCD的對象化封裝,并且可以cancel,推薦使用,大名鼎鼎的AFNetworking就是用這個的
GCD蘋果說推薦使用,在不方便使用NSOperation的就用吧,比如延時執(zhí)行等等
下面這篇文章寫了iOS各種多線程技術的對比,還不錯,可以看看
關于iOS多線程,你看我就夠了
類定義
去掉了一些不常用或者難理解的內容。大致的用法,看看屬性和方法的名字應該了解。
@interface NSOperation : NSObject
- (void)start;
- (void)main;
- (void)cancel;
- (void)addDependency:(NSOperation *)op;
- (void)removeDependency:(NSOperation *)op;
- (void)waitUntilFinished NS_AVAILABLE(10_6, 4_0);
@property (readonly, getter=isCancelled) BOOL cancelled;
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isAsynchronous) BOOL asynchronous NS_AVAILABLE(10_8, 7_0);
@property (readonly, getter=isReady) BOOL ready;
@property (readonly, copy) NSArray<NSOperation *> *dependencies;
@property (nullable, copy) void (^completionBlock)(void) NS_AVAILABLE(10_6, 4_0);
@property (nullable, copy) NSString *name NS_AVAILABLE(10_10, 8_0);
@end
@interface NSBlockOperation : NSOperation
+ (instancetype)blockOperationWithBlock:(void (^)(void))block;
- (void)addExecutionBlock:(void (^)(void))block;
@property (readonly, copy) NSArray<void (^)(void)> *executionBlocks;
@end
@interface NSOperationQueue : NSObject
- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block NS_AVAILABLE(10_6, 4_0);
- (void)cancelAllOperations;
- (void)waitUntilAllOperationsAreFinished;
@property (readonly, copy) NSArray<__kindof NSOperation *> *operations;
@property (readonly) NSUInteger operationCount NS_AVAILABLE(10_6, 4_0);
@property NSInteger maxConcurrentOperationCount;
@property (getter=isSuspended) BOOL suspended;
@property (nullable, copy) NSString *name NS_AVAILABLE(10_6, 4_0);
@property (class, readonly, strong, nullable) NSOperationQueue *currentQueue NS_AVAILABLE(10_6, 4_0);
@property (class, readonly, strong) NSOperationQueue *mainQueue NS_AVAILABLE(10_6, 4_0);
@end
至于NSInvocationOperation,swift版本去掉了,先不管吧,block形式更優(yōu)雅
簡單使用
-
NSOperation是個抽象類,并不具備封裝操作的能力,必須使用它的子類。 -
NSInvocationOperation使用不方便,可以不考慮 -
NSBlockOperation推薦使用,block比較方便,也是蘋果推薦的方式,不過要注意一下引用循環(huán)的問題 - 自定義
NSOperation,重寫main方法,看情況使用 - 結合
NSOperationQueue一起使用。queue、operation、block三級關系要搞清楚。 -
queue默認是并行的,maxConcurrentOperationCount設為 1,就是串行的; 默認值是-1,不能設置0 -
operation默認是異步執(zhí)行的,- (void)addDependency:(NSOperation *)op;就可以變成同步的,按照指定順序執(zhí)行,要注意避免相互依賴,形成死鎖 - 除了同一
quere操作間建立依賴關系,當然也可以在不同queue的NSOperation之間創(chuàng)建依賴關系 -
block是異步的,并行的,目前還沒有看到改變這種默認行為的方式。如果有需要,保持一個operation中只有一個block,通過控制operation來達到控制block的目的 -
operation可以單獨使用,單獨使用時,默認是在調用start方法的相同線程同步執(zhí)行。所以還是推薦放在一個NSOperationQueue中,方便集中管理,并且默認是異步執(zhí)行 - 默認情況下,
NSOperation單獨使用時系統同步執(zhí)行操作,并沒有開辟新線程的能力,從這個方面講,也推薦將NSOperation放在一個NSOperationQueue中使用,不推薦單獨使用 -
[NSOperationQueue mainQueue]代表主線程
例子1
// 1.創(chuàng)建隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// maxConcurrentOperationCount 默認等于 -1, 代表不限制, 可以創(chuàng)建N多線程
// 默認就是并發(fā)
// 如果想實現串行, 那么就設置maxConcurrentOperationCount = 1
// queue.maxConcurrentOperationCount = 1;
// 2.創(chuàng)建任務
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1];
NSLog(@"1 = %@", [NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1];
NSLog(@"2 = %@", [NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1];
NSLog(@"3 = %@", [NSThread currentThread]);
}];
// 3.將任務添加到隊列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
NSThread用來sleep,獲取當前線程的函數很好用
例子2
// 1.開啟子線程下載圖片
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
// 子線程
NSString *urlStr = @"https://www.baidu.com/img/bd_logo1.png";
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2.生成下載好的圖片
UIImage *image = [UIImage imageWithData:data];
// 3.回到主線程更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"更新UI");
// 主線程
self.imageView.image = image;
}];
}];
這里把
operation匿名了,簡化了操作。這個和GCD的經典用法很類似,全部是面向對象的,簡化很多。
例子3
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSOperationQueue *queue2 = [[NSOperationQueue alloc] init];
__block UIImage *image1 = nil;
__block UIImage *image2 = nil;
// 1.開啟一個線程下載第一張圖片
NSOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://cdn.cocimg.com/assets/images/logo.png?v=201510272"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2.生成下載好的圖片
UIImage *image = [UIImage imageWithData:data];
image1 = image;
}];
// 2.開啟一個線程下載第二長圖片
NSOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/img/bd_logo1.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2.生成下載好的圖片
UIImage *image = [UIImage imageWithData:data];
image2 = image;
}];
// 3.開啟一個線程合成圖片
NSOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[image1 drawInRect:CGRectMake(0, 0, 100, 200)];
[image2 drawInRect:CGRectMake(100, 0, 100, 200)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 4.回到主線程更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"回到主線程更新UI");
self.imageView.image = newImage;
}];
}];
// 監(jiān)聽任務是否執(zhí)行完畢
op1.completionBlock = ^{
NSLog(@"第一張圖片下載完畢");
};
op2.completionBlock = ^{
NSLog(@"第二張圖片下載完畢");
};
// 添加依賴
// 只要添加了依賴, 那么就會等依賴的任務執(zhí)行完畢, 才會執(zhí)行當前任務
// 注意:
// 1.添加依賴, 不能添加循環(huán)依賴
// 2.NSOperation可以跨隊列添加依賴
[op3 addDependency:op1];
[op3 addDependency:op2];
// 將任務添加到隊列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue2 addOperation:op3];
}
下面這篇文章寫的不錯,上面3個例子都來自這里
iOS NSOperation