4

我有一个NSOperation要同时运行的子类。

我的理解是,要使并发操作起作用:

  • 我需要定义isConcurrent返回YES
  • 我需要定义start方法
  • 我需要在完成时发送 KVOisExecuting通知isFinished
  • 当和@synthesize的值发生更改时,使用将自动发送适当的 KVO 通知。isExecutingisFinished

尽管如此,我已经验证我的队列永远不会移动到下一个项目。

这是我的代码的主要内容:

@interface MyOperation()

@property (readwrite) BOOL isExecuting;
@property (readwrite) BOOL isFinished;

@end

@implementation MyOperation

- (void)start
{
    @autoreleasepool {
        self.isExecuting = YES;
        self.HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];

        _HTTPOperation.completionBlock = [^{
            [self completed];

            self.isExecuting = NO;
            self.isFinished = YES;
        } copy];

        [_HTTPOperation start];
    }
}

- (BOOL)isConcurrent
{
    return YES;
}

- (void)completed
{
}

@end

我错过了什么?

(这是在 iPhone 上,但我无法想象这很重要。)

4

2 回答 2

4

看起来无论 KVO 通知@synthesize发送什么都不足以NSOperationQueue继续前进。

手动发送通知可以解决问题:

- (void)start
{
    @autoreleasepool {
        [self willChangeValueForKey:@"isExecuting"];
        self.isExecuting = YES;
        [self didChangeValueForKey:@"isExecuting"];

        NSURLRequest *URLRequest = [self buildRequest];
        if (!URLRequest) {
            [self willChangeValueForKey:@"isFinished"];
            [self willChangeValueForKey:@"isExecuting"];
            _isExecuting = NO;
            _isFinished = YES;
            [self didChangeValueForKey:@"isExecuting"];
            [self didChangeValueForKey:@"isFinished"];
            return;
        }

        self.HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];

        _HTTPOperation.completionBlock = [^{
            [self completed];

            [self willChangeValueForKey:@"isFinished"];
            [self willChangeValueForKey:@"isExecuting"];
            _isExecuting = NO;
            _isFinished = YES;
            [self didChangeValueForKey:@"isExecuting"];
            [self didChangeValueForKey:@"isFinished"];
        } copy];

        [_HTTPOperation start];
    }
}

也可以看看:

于 2012-05-02T23:30:22.337 回答
1

你的“队列”是什么样的?你在使用 NSOperationQueue 吗?

无论如何,我会尝试用我的理解回答你的问题:P

我会为我的 NSOperation 创建一个委托,并让KVO负责调用它。

比如说你的NSOperation类看起来像这样

@interface MyOperation : NSOperation

@property (assign) id<MyOperationDelegate> delegate;

您的实施

@synthesize delegate;
@synthesize error;

    -(id)init{
    self = [super init];
    if(self){
        [self addObserver:self forKeyPath:@"isFinished" 
                  options:NSKeyValueObservingOptionNew 
                  context:NULL];
    }
    return self;
}

-(void)dealloc{
    [self removeObserver:self forKeyPath:@"isFinished"];
    [super dealloc];
}

-(void)observeValueForKeyPath:(NSString *)keyPath 
                     ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    if([keyPath isEqualToString:@"isFinished"] == YES){
    if([self isCancelled] == NO){
        if(delegate != nil && [delegate respondsToSelector:@selector(operationComplete:)]){
            [delegate taskComplete:self];
        }
    }else{
        if(delegate != nil && [delegate respondsToSelector:@selector(operationCancelled)]){
            [delegate taskCancelled];
        }
    }
}

}

-(void)main{
    [NSException exceptionWithName:kTaskException 
                            reason:@"Only to be used with subclass" 
                          userInfo:nil];
}

最后是你的协议

@class MyOperation;
@protocol MyOperationDelegate <NSObject>

@optional
-(void)operationComplete:(MyOperation*)operation;
-(void)operationCancelled;
于 2012-05-05T02:12:24.040 回答