自定义operation

相比GCD,可以中断任务,也可使用 addDependency,对要执行的任务进行排序..

//
// CustomOperation.h
// Test
//
// Created by M on 16/1/5.
// Copyright © 2016年 Meng. All rights reserved.
// #import <Foundation/Foundation.h> @protocol CustomOperationDelegate <NSObject> -(void)handleDelegate:(NSString*)str; @end @interface CustomOperation : NSOperation @property(nonatomic,assign)id<CustomOperationDelegate> Delegate;
@property(nonatomic)BOOL IsStopOperation; -(id)initWithOperationDelegate:(id<CustomOperationDelegate>) delegate; @end
//
// CustomOperation.m
// Test
//
// Created by M on 16/1/5.
// Copyright © 2016年 Meng. All rights reserved.
// #import "CustomOperation.h" @implementation CustomOperation -(id)initWithOperationDelegate:(id<CustomOperationDelegate>)delegate
{ if (self = [super init]) { self.Delegate = delegate; } return self; } -(void)main
{
@autoreleasepool { if ([self.Delegate respondsToSelector:@selector(handleDelegate:)]) { for (int i = ; i<; i++) { //中断operation
if (self.IsStopOperation) {
return;
} sleep(); [self.Delegate performSelector:@selector(handleDelegate:) withObject:[NSString stringWithFormat:@"CustomOperation,%d",i]]; }
} }
} @end

ViewController实现

//
// ViewController.m
// Test
//
// Created by M on 16/1/4.
// Copyright © 2016年 Meng. All rights reserved.
// #import "ViewController.h"
#import "CustomOperation.h" @interface ViewController ()<CustomOperationDelegate>//实现委托
{
CustomOperation *CO;
UILabel *CountLbl;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self setupView]; NSOperationQueue *qu = [[NSOperationQueue alloc] init]; CO = [[CustomOperation alloc] initWithOperationDelegate:self]; [qu addOperation:CO];//添加到Queue #pragma mark ======================= NSBlockOperation *BKO1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"BKO1,%@", [NSThread currentThread]); for (int i = ; i<; i++) {
NSLog(@"111111:%d",i);
sleep();
}
}]; NSBlockOperation *BKO2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"BKO2,%@", [NSThread currentThread]); for (int i = ; i<; i++) {
NSLog(@"222222:%d",i);
sleep();
}
}]; // BKO2 依赖 BKO1
[BKO2 addDependency:BKO1]; [qu addOperation:BKO1];
[qu addOperation:BKO2]; #pragma mark ======================= NSOperationQueue *qu2 = [[NSOperationQueue alloc] init];
//把任务队列的最大并发数设置为1,也可以进行执行的排序....只不过是否有其他问题,还请指教.
qu2.maxConcurrentOperationCount = ; for (int i = ; i < ; i ++) { [qu2 addOperation:[NSBlockOperation blockOperationWithBlock:^{
sleep();
NSLog(@"BKO:%d",i);
}]]; } } -(void)setupView
{ CountLbl = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/, self.view.bounds.size.height/, self.view.bounds.size.width/, )]; CountLbl.textAlignment = NSTextAlignmentCenter; [self.view addSubview:CountLbl]; UIButton *stopBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/, self.view.bounds.size.height/-, self.view.bounds.size.width/, )]; stopBtn.backgroundColor = [UIColor darkGrayColor]; [stopBtn setTitle:@"Stop" forState:UIControlStateNormal]; [self.view addSubview:stopBtn]; [stopBtn addTarget:self action:@selector(StopOperation) forControlEvents:UIControlEventTouchUpInside]; } -(void)StopOperation
{ CO.IsStopOperation = YES;
}
#pragma mark Delegate
-(void)handleDelegate:(NSString *)str
{
//update the label text dispatch_async(dispatch_get_main_queue(), ^{
CountLbl.text = str;
}); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

NSOperation-LMLPHP

04-27 03:21