继承NSOperation-自定义异步 Operation

22 篇文章 0 订阅

转自:http://blog.sina.com.cn/s/blog_7b9d64af0101ca7q.html


记得做异步操作多线程操作时,有关于继承 NSOperation自定义同步Operation的操作。
当然也应该有继承 NSOperation 自定义同步Operation的操作。
今天研究了一下。mark!!!

关于继承NSOperation-自定义 同步  Operation 的操作,可以先看看我的另一篇博文:


今天研究了一下。mark!!

1.自定义自己的 NSOperation 操作类

该类跟同步方法的自定义类是一模一样的道理。

SimpleOperation.h 文件

@interface SimpleOperation : NSOperation{

    NSString *givenObject;

    BOOL finished;

    BOOL executing;


}


// 定义初始化方法

- (id) initWithObject:(NSObject *)paramObject;


@end


SimpleOperation.m 文件

#import "SimpleOperation.h"


@implementation SimpleOperation



- (id) init {

    NSNumber *dummyObject = [NSNumber numberWithInteger:123];

    return([self initWithObject:dummyObject]);


}



// 定义初始化方法

- (id) initWithObject:(NSString *)paramObject{

    self = [super init];

    if (self != nil){

        givenObject = paramObject;

        NSLog(@"%@",paramObject);

    }

    return(self);

}

- (void) start {


    if ([self isCancelled]){

        [self willChangeValueForKey:@"isFinished"];

        finished = YES;

        [self didChangeValueForKey:@"isFinished"];

        return;

    } else {

        

        [self willChangeValueForKey:@"isExecuting"];

        executing = YES;

        [self main];

        [self didChangeValueForKey:@"isExecuting"];

        

    }

}

- (void) main {

    @try {

        @autoreleasepool {

            BOOL taskIsFinished = NO;

            while (taskIsFinished == NO &&[self isCancelled] == NO){

                NSLog(@"%s"__FUNCTION__);

                NSLog(@"Parameter Object = %@", givenObject);

                NSLog(@"Main Thread = %@", [NSThread mainThread]);

                NSLog(@"Current Thread = %@", [NSThread currentThread]);

                                                                                                                                                                         

                taskIsFinished = YES;

            }

            

            [self willChangeValueForKey:@"isFinished"];

            [self willChangeValueForKey:@"isExecuting"];

            finished = YES;

            executing = NO;

            [self didChangeValueForKey:@"isFinished"];

            [self didChangeValueForKey:@"isExecuting"]; }

    }

    @catch (NSException * e) {

        NSLog(@"Exception %@", e);

    }

}


// 跟同步不同的地方。

// 必须重载Operation的实例方法 isConcurrent并在该方法中返回 YES

- (BOOL) isConcurrent{

    return YES;

}


- (BOOL) isFinished{

    

    return finished;

}


- (BOOL) isExecuting{

    

    return executing;

}



@end


2.使用 NSOperationQueue 执行异步Operation

#import

@class AppDelegate;

@class SimpleOperation;

@class CountingOperation;

@interface MoreViewController : UIViewController{

    

}


@property (nonatomic, retain) NSOperationQueue *operationQueue;

@property (nonatomic, retain) SimpleOperation *firstOperation;

@property (nonatomicretainSimpleOperation *secondOperation;

@property (nonatomic, retain) SimpleOperation *threeOperation;

@property (nonatomic, retain) CountingOperation *countOperation;


@end


#import "SimpleOperation.h"

#import "CountingOperation.h"


@implementation MoreViewController

@synthesize operationQueue;

@synthesize firstOperation;

@synthesize secondOperation;

@synthesize threeOperation;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

        // Custom initialization

    }

    return self;

}


- (void)dealloc{

    [super dealloc];

}




- (void)viewDidLoad{

    [super viewDidLoad];

    

    CGRect btnControlRect=CGRectMake(2005010030);

    UIButton *btnControl=[[UIButton allocinitWithFrame:btnControlRect];

    

    [btnControl setBackgroundColor:[UIColor redColor]];

    

    [btnControl addTarget:self action:@selector(ClickControlAction:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:btnControl];

    [btnControl release];

    

  


}


- (void)ClickControlAction:(id)sender{

    NSLog(@"ClickControlAction");



}


- (void)viewDidAppear:(BOOL)animated{

    

    NSString *firstString=[[NSString allocinitWithFormat:@"firstString"];

    NSString *secondString=[[NSString allocinitWithFormat:@"secondString"];

    NSString *threeString=[[NSString allocinitWithFormat:@"threeString"];

    // 初始化自定义的NSOperation对象

    self.firstOperation = [[SimpleOperation alloc] initWithObject:firstString];

    self.secondOperation = [[SimpleOperation alloc] initWithObject:secondString];

    self.threeOperation = [[SimpleOperation alloc] initWithObject:threeString];

    self.countOperation=[[CountingOperation alloc] initWithStartingCount:0endingCount:10000];

    self.operationQueue = [[NSOperationQueue alloc] init];

    // 将自定义的Operation添加到 NSOperationQueue

    [self.operationQueue addOperation:self.firstOperation];

    [self.operationQueue addOperation:self.secondOperation];

    [self.operationQueue addOperation:self.threeOperation];

     [self.operationQueue addOperation:self.countOperation];

    NSLog(@"Main thread is here");

     

}





- (void)viewDidDisappear:(BOOL)animated{

    

    [self.firstOperation release];

    [self.secondOperation release];

    [self.operationQueue release];


}






- (void)viewDidUnload{

    

    [super viewDidUnload];


}



@end


输出:

 firstString

 secondString

 threeString

 Main thread is here

 -[SimpleOperation main]

 -[SimpleOperation main]

 -[SimpleOperation main]

 Parameter Object = firstString

 Parameter Object = secondString

 Parameter Object = threeString

 Main Thread = {name = (null), num = 1}

 Main Thread = {name = (null), num = 1}

 Main Thread = {name = (null), num = 1}

 Current Thread = {name = (null), num = 9}

 Current Thread = {name = (null), num = 7}

 Main Thread = {name = (null), num = 1}

 Current Thread = {name = (null), num = 8}

 Current Thread = {name = (null), num = 9}

 DemoVideo[29240:290b] Count = 0

……

 Count = 9999


看到飘红的地方,是在不同的线程中执行的!

这样,就不会阻断主线程!
太多的理论已经说的很多,就不在赘述了!

自定义异步Operation依赖

// 同理,自定义的SimpleOperation也是可以进行依赖设置

    [self.firstOperation addDependency:self.secondOperation];

    [self.secondOperation addDependency:self.threeOperation];

    [self.threeOperation addDependency:self.countOperation];


一句话:强大,很强大!!!

希望对你有所帮助!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值