执行NSOperation时的状态是什么

时间:2017-05-31 14:00:36

标签: ios objective-c nsoperation nsoperationqueue

我已经完成了NSOperation演示示例,在阅读了文档和博客后我才知道添加到队列的任何操作何时都有不同的状态

  

准备→执行→完成

来自viewController.m代码的演示,如

#import "OperationViewController.h"
#import "TestOperation.h"
#import "AppDelegate.h"

@interface OperationViewController ()
@property(nonatomic,strong) NSOperationQueue *queue;
@property(nonatomic,strong) NSMutableArray *oprationArray;
@property(nonatomic,strong) TestOperation *operation1;
@property(nonatomic,strong) TestOperation *operation2;
@property(nonatomic,strong) TestOperation *operation3;
@property(nonatomic,strong) TestOperation *operation4;
@property(nonatomic,strong) TestOperation *operation5;
@end

static char CharForCurrentThread(void)
// Returns 'M' if we're running on the main thread, or 'S' otherwies.
{
    return [NSThread isMainThread] ? 'M' : 'S';
}

@implementation OperationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //Case-1 First Initalisation
    _queue = [[NSOperationQueue alloc] init];
    _queue.maxConcurrentOperationCount = 4;

    _operation1 = [[TestOperation alloc] init];
    _operation1.countValue = 1;
    //op.name = [NSString stringWithFormat:@"Operation-%d",i];
    [_operation1 setValue:[NSString stringWithFormat:@"Operation-%ld",(long)_operation1.countValue] forKey:@"name"]; //==

    _operation2 = [[TestOperation alloc] init];
    _operation2.countValue = 2;
    [_operation2 setValue:[NSString stringWithFormat:@"Operation-%ld",(long)_operation2.countValue] forKey:@"name"]; //==

    _operation3 = [[TestOperation alloc] init];
    _operation3.countValue = 3;
    //_operation3.queuePriority = NSOperationQueuePriorityVeryHigh;
    [_operation3 setValue:[NSString stringWithFormat:@"Operation-%ld",(long)_operation3.countValue] forKey:@"name"]; //==

    _operation4 = [[TestOperation alloc] init];
    _operation4.countValue = 4;
    [_operation4 setValue:[NSString stringWithFormat:@"Operation-%ld",(long)_operation4.countValue] forKey:@"name"]; //==

    _operation5 = [[TestOperation alloc] init];
    _operation5.countValue = 5;
    //_operation5.queuePriority = NSOperationQueuePriorityLow;
    [_operation5 setValue:[NSString stringWithFormat:@"Operation-%ld",(long)_operation5.countValue] forKey:@"name"]; // KVC complient

    //[_operation1 addDependency:_operation5];

//    [_operation5 addObserver:self forKeyPath:@"isFinished" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
//    [_operation5 addObserver:self forKeyPath:@"isExecuting" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

//    _oprationArray = [NSMutableArray arrayWithObjects:_operation1,_operation2,_operation3,_operation4,_operation5, nil];
    _oprationArray = [NSMutableArray arrayWithObjects:_operation1,_operation2,_operation3, nil];

    [_queue addOperations:_oprationArray waitUntilFinished:NO];
}

@end

和自定义操作类如

#import "TestOperation.h"

@implementation TestOperation

- (void)main
{
    NSLog(@"Method main :%ld",(long)_countValue);
    NSLog(@"Operation Priority ::%ld",(long)self.queuePriority);
    if ([self isCancelled])
    {
        NSLog(@"Operation is Canceled ::%@",self.name);
    }

    if ([self isExecuting])
    {
        NSLog(@"Operation is Executing ::%@",self.name);
    }

    if ([self isFinished])
    {
        NSLog(@"Operation is Finished ::%@",self.name);
    }

    if ([self isConcurrent])
    {
        NSLog(@"Operation is Concurrent ::%@",self.name);
    }

    if ([self isAsynchronous])
    {
        NSLog(@"Operation is Asynchronous ::%@",self.name);
    }

    if ([self isReady])
    {
        NSLog(@"Operation is Ready ::%@",self.name);
    }    
}

-(BOOL)finished
{
    NSLog(@"Operation is Finished ::%@",self.name);
    return YES;
}

@end

根据文档,我期待上面提到的状态(Ready - > Executing - > Finished),Even输出不显示完成状态 但是在如下的控制台中:

2017-05-31 19:18:33.212 DemoData[16365:485789] Method main :1
2017-05-31 19:18:33.212 DemoData[16365:485919] Method main :2
2017-05-31 19:18:33.212 DemoData[16365:485921] Method main :4
2017-05-31 19:18:33.212 DemoData[16365:485920] Method main :3
2017-05-31 19:18:33.213 DemoData[16365:485789] Operation Priority ::0
2017-05-31 19:18:33.214 DemoData[16365:485919] Operation Priority ::0
2017-05-31 19:18:33.214 DemoData[16365:485921] Operation Priority ::0
2017-05-31 19:18:33.215 DemoData[16365:485920] Operation Priority ::0
2017-05-31 19:18:33.215 DemoData[16365:485789] Operation is Executing ::Operation-1
2017-05-31 19:18:33.216 DemoData[16365:485919] Operation is Executing ::Operation-2
2017-05-31 19:18:33.216 DemoData[16365:485921] Operation is Executing ::Operation-4
2017-05-31 19:18:33.217 DemoData[16365:485920] Operation is Executing ::Operation-3
2017-05-31 19:18:33.217 DemoData[16365:485789] Operation is Ready ::Operation-1
2017-05-31 19:18:33.218 DemoData[16365:485919] Operation is Ready ::Operation-2
2017-05-31 19:18:33.218 DemoData[16365:485921] Operation is Ready ::Operation-4
2017-05-31 19:18:33.220 DemoData[16365:485920] Operation is Ready ::Operation-3
2017-05-31 19:18:33.221 DemoData[16365:485789] Method main :5
2017-05-31 19:18:33.240 DemoData[16365:485789] Operation Priority ::0
2017-05-31 19:18:33.241 DemoData[16365:485789] Operation is Executing ::Operation-5
2017-05-31 19:18:33.241 DemoData[16365:485789] Operation is Ready ::Operation-5
2017-05-31 19:18:33.242 DemoData[16365:485789] queue has completed

0 个答案:

没有答案