继承NSOperation-自定义同步 operation

22 篇文章 0 订阅

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


记得上一次,我写了关于 NSInvocationOperation类和 NSBlockOperation类的关于定义同步 O peration的一些入门级别的介绍。
可以参考如下链接:


可以看到, NSInvocationOperation 类和 NSBlockOperation 都继承自 NSOperation类。
所以,他们定义了自己的方法和规范来进行同步的 O peration操作。
当然,我们也可以通过继承 NSOperation类,来定义我们自己的同步 O peration

贴代码:

1. CountingOperation.h 继承了NSOperation

@interface CountingOperation : NSOperation{



}


 

- (id) initWithStartingCount:(NSUInteger)paramStartingCount endingCount:(NSUInteger)paramEndingCount;


@end


2. CountingOperation.m 实现了我们相应的操作逻辑

@implementation CountingOperation


NSUInteger startingCount;

NSUInteger endingCount;

BOOL finished;

BOOL executing;


- (id) init {

    

    return([self initWithStartingCount:0 endingCount:1000]);

    

}


// 我们为我们自定义的operation,提供的初始化方法。这个初始化方法,根据我们的需要进行自定义

- (id) initWithStartingCount:(NSUInteger)paramStartingCount endingCount:(NSUInteger)paramEndingCount{

    self = [super init];

    if (self != nil){

        // 用参数的方式,初始化我们需要的数据,将在main中使用

        startingCount = paramStartingCount;

        endingCount = paramEndingCount;

    }

    return(self);

}


- (void) start {

    // 如果我们取消了在开始之前,我们就立即返回并生成所需的KVO通知

    if ([self isCancelled]){

        // 我们取消了该 operation,那么就要告诉KVO,该operation已经执行完成(isFinished)。这样,调用的队列(或者线程)会继续执行。

        [self willChangeValueForKey:@"isFinished"];

        finished = YES;

        [self didChangeValueForKey:@"isFinished"];

        

        return;

    } else {

        // 没有取消,那就要告诉KVO,该队列开始执行了(isExecuting)!那么,就会调用main方法,进行同步执行。

        [self willChangeValueForKey:@"isExecuting"];

        executing = YES;

        [self main];// 执行main方法

        [self didChangeValueForKey:@"isExecuting"];

    }

}


- (void) main {

    @try {

        // 我们必须为我们自定义的operation 提供 autorelease pool,因为 operation 完成后需要销毁。

        @autoreleasepool {

            // 提供一个变量标识,来表示我们需要执行的操作是否完成了,当然,没开始执行之前,为NO

            BOOL taskIsFinished = NO;

            // while 保证:只有当没有执行完成和没有被取消,才执行我们自定义的相应操作

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

                

                // 我们自定义的操作


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

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

                

                // 使用我们初始化传递来的参数:startingCountendingCount

                NSUInteger counter = startingCount;

                for (counter = startingCount;counter < endingCount;counter++){

                    

                    NSLog(@"Count = %lu", (unsigned long)counter);

                }

                // 这里,设置我们相应的操作都已经完成,后面就是要通知KVO我们的操作完成了。

                

                taskIsFinished = YES;

            }

            // KVO 生成通知,告诉其他线程,该operation 执行完了

            [self willChangeValueForKey:@"isFinished"];

            [self willChangeValueForKey:@"isExecuting"];

            finished = YES;

            executing = NO;

            [self didChangeValueForKey:@"isFinished"];

            [self didChangeValueForKey:@"isExecuting"];

            

        } 

    }

    @catch (NSException * e) {

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

    }

}


// 返回该operation是否完成

- (BOOL) isFinished{

    return(finished);

}


// 返回该operation是否在执行

- (BOOL) isExecuting{

    return(executing);

}


// 1.使用自己定义的同步operation,需要继承自NSOperation,并实现必要的方法:isFinishedisExecutingmain等,并实现KVO机制

// 2.如果不想让你自定义的operation与其他operation进行异步操作,你可以手动开始(调用start方法),并且在operationstart方法里面简单的调用main方法。

// 3.如果要自定义operation,需要继承资NSOperation。并且重载 isExecuting方法和isFinished方法。在这两个方法中,必须返回一个线程安全值(通常是个BOOL值),这个值可以在 operation 中进行操作。

// 4.一旦你的 operation 开始了,必须通过 KVO,告诉所有的监听者,现在该operation的执行状态。

// 5. operation  main 方法里面,必须提供 autorelease pool,因为你的 operation 完成后需要销毁。

// 6.必须为我们自定义的 operation 提供一个初始化方法。如:initWithStartingCount 方法。




@end


3.调用:在其他类中初始化我们自定义的CountingOperation类并手动start。

- (void)viewDidLoad{

    [super viewDidLoad];

    

    self.simpleOperation = [[CountingOperation alloc] initWithStartingCount:0endingCount:1000];

//    [self.simpleOperation cancel];// 在开始前,我们可以取消该 operation

    [self.simpleOperation start];

    NSLog(@"只有在我们定义的同步Operation被取消或者执行完成后,才会进行后面的操作");


}


解释也都附带在里面了!爽啊!
异步呢?!自定义异步 Operation类应该登场了吧!我都迫不及待了!呵呵!

希望对你有所帮助!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值