NSOperation

自定义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 = 0 ; i<10000; i++) {
                
                //中断operation
                if (self.IsStopOperation) {
                    return;
                }
                
                sleep(1);
                
                [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 = 1; i<10; i++) {
            NSLog(@"111111:%d",i);
            sleep(1);
        }
    }];
    
    NSBlockOperation *BKO2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"BKO2,%@", [NSThread currentThread]);
        
        for (int i = 1; i<10; i++) {
            NSLog(@"222222:%d",i);
            sleep(1);
        }
    }];
    
    // BKO2 依赖 BKO1
    [BKO2 addDependency:BKO1];
    
    [qu addOperation:BKO1];
    [qu addOperation:BKO2];
    
    
#pragma mark =======================
    
    NSOperationQueue *qu2 = [[NSOperationQueue alloc] init];
    //把任务队列的最大并发数设置为1,也可以进行执行的排序....只不过是否有其他问题,还请指教.
    qu2.maxConcurrentOperationCount = 1;
    
    for (int i = 0; i < 100; i ++) {
        
        [qu2 addOperation:[NSBlockOperation blockOperationWithBlock:^{
            sleep(1);
            NSLog(@"BKO:%d",i);
        }]];

    }
    

    
  
   
}

-(void)setupView
{
    
    CountLbl = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/4, self.view.bounds.size.height/2, self.view.bounds.size.width/2, 30)];
    
    CountLbl.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:CountLbl];
    
    
    UIButton *stopBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/4, self.view.bounds.size.height/2-60, self.view.bounds.size.width/2, 30)];
    
    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

 

 

posted @ 2016-01-05 10:12  NGI.  阅读(169)  评论(0编辑  收藏  举报
GitHub | M1989