1.創建自定義Operation類,繼承自NSOperation
2.重寫自定義Operation的main方法
重寫- (void)main方法,在里面實現想執行的任務
創建自動釋放池(因為如果是異步操作,無法訪問主線程的自動釋放池),新版本已經不再需要手動創建自動釋放池
3.通過 "- (BOOL)isCancelled" 方法檢測操作是否被取消,對取消做出響應
之前提到過NSOperation只能對還未執行的任務做取消操作,是由于系統封裝的方法內部我們無法訪問,而自定義Operation中,我們需要重寫main方法,在這里檢測到isCancelled屬性為YES時,直接返回,不執行后面的操作即可取消正在執行的操作,雖然能夠實現取消正在執行的操作,但并不是立刻停止的,而是在調用了 operation 的 cancel 方法之后的下一個 isCancelled 的檢查點取消的
4.在controller中調用start方法,或者添加到隊列, main方法會被調用
- 通過自定義Operation, 模擬異步下載圖片
1.自定義DownloadOperation類,繼承自NSOperation
2.聲明屬性
// url地址
@property (nonatomic,copy) NSString *urlString;
// 完成回調
@property (nonatomic,copy) void(^completeHandler)(UIImage *img);
3.在DownloadOperation類中重寫main方法:
#import "DownloadOperation.h"
@implementation DownloadOperation
- (void)main{
//@autoreleasepool {
// 更新后不再需要手動創建,系統內部做過優化
//}
NSAssert(self.completeHandler != nil, @"completeHandler == nil");
// 下載圖片
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlString]];
UIImage *image = [UIImage imageWithData:data];
// 取消操作
if (self.isCancelled) {
NSLog(@"取消操作");
return;
}
// 返回主線程刷新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.completeHandler(image);
}];
}
@end
4.使用:
#import "ViewController.h"
#import "DownloadOperation.h"
@interface ViewController ()
@end
@implementation ViewController{
NSOperationQueue *_queue;
UIImageView *_imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
_queue = [[NSOperationQueue alloc] init];
_imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_imageView.contentMode = UIViewContentModeCenter;
[self.view addSubview:_imageView];
// 1.創建自定義的Operation對象 (下載圖片操作)
DownloadOperation *operation = [[DownloadOperation alloc] init];
operation.urlString = @"http://t1.mmonly.cc/mmonly/2014/201407/129/7.jpg";
[operation setCompleteHandler:^(UIImage *img) {
// 更新UI
_imageView.image = img;
}];
// 2.把操作添加到隊列中
[_queue addOperation:operation];
}
@end
示例代碼中先通過alloc]init實例化了操作對象,再通過屬性完成參數傳遞和下載圖片的回調
在使用系統為我們提供的NSOperation子類中,系統提供了類方法直接使用,所以為了方便使用,同樣可以自定義一個類方法,完成參數傳遞和回調, 結合操作的取消一同進行演示
代碼優化(自定義類方法):
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DownloadOperation : NSOperation
// url地址
@property (nonatomic,copy) NSString *urlString;
// 完成回調
@property (nonatomic,copy) void(^completeHandler)(UIImage *img);
// 下載圖片類方法
+(instancetype)downloadImageUrlString:(NSString *)urlString completeHandler:(void(^)(UIImage *img))completeHandler;
@end
.m
#import "DownloadOperation.h"
@implementation DownloadOperation
- (void)main{
NSAssert(self.completeHandler != nil, @"completeHandler == nil");
// 下載圖片
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlString]];
UIImage *image = [UIImage imageWithData:data];
// 取消操作
if (self.isCancelled) {
NSLog(@"取消操作");
return;
}
// 返回主線程刷新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.completeHandler(image);
}];
}
// 自定義類方法下載圖片
+ (instancetype)downloadImageUrlString:(NSString *)urlString completeHandler:(void (^)(UIImage *))completeHandler{
DownloadOperation *operation = [[DownloadOperation alloc] init];
operation.urlString = urlString;
operation.completeHandler = completeHandler;
return operation;
}
@end
外界調用:
#import "ViewController.h"
#import "DownloadOperation.h"
@interface ViewController ()
@end
@implementation ViewController{
NSOperationQueue *_queue;
}
- (void)viewDidLoad {
[super viewDidLoad];
_queue = [[NSOperationQueue alloc] init];
_queue.maxConcurrentOperationCount = 3;
for (int i = 0; i < 20; i ++) {
// 1.創建自定義的Operation對象
DownloadOperation *operation = [DownloadOperation downloadImageUrlString:@"http://t1.mmonly.cc/uploads/tu/201607/tt/1alqhs1gwxo.jpg" completeHandler:^(UIImage *img) {
NSLog(@"%d --> (%@)",i,[NSThread currentThread]);
}];
// 2.把操作添加到隊列中
[_queue addOperation:operation];
}
}
// 觸摸屏幕時取消所有操作(包括正在執行的操作)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_queue cancelAllOperations];
}
@end