【问题标题】:NSOperation with dependency not being executed when added to NSOperationQueueNSOperation 添加到 NSOperationQueue 时没有执行依赖项
【发布时间】:2012-02-02 12:10:30
【问题描述】:

我试图弄清楚为什么在添加到 NSOperationQueue(iOS5,ARC)时没有执行具有依赖关系的 NSOperation:

@implementation NSOperationTest {
    NSOperationQueue *_operationQueue;
}

- (id)init {
    self = [super init];
    if (self) {
        _operationQueue = [[NSOperationQueue alloc] init];
    }
    return self;
}

-(void) test
{
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"op1 running");
    }];

    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"op2 running");
    }];

    [op2 addDependency:op1];
    [_operationQueue addOperation:op2];
}
@end

这让我抓狂了,这里op1 应该在op2 之前执行,但是在没有依赖关系的情况下添加任何一个都可以正常工作。有人知道为什么吗?

提前致谢。

【问题讨论】:

    标签: iphone cocoa-touch ios5 nsoperation nsoperationqueue


    【解决方案1】:

    您必须显式地对操作队列进行任何操作(或直接执行)才能执行 - 依赖项也不例外。如果不这样做(正如你所做的那样)op2 将永远不会执行,因为它正在等待op1 执行,而op1 从未被告知执行。因此,通过在 test 方法的末尾添加 [_operationQueue addOperation: op1];,您的问题应该得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多