我创建了自己的 NSOperation 子类,现在我希望它的一些实例并行运行。因为这是一个并发操作,我已经覆盖了
- (void)start {
[self willChangeValueForKey:@"isExecuting"];
isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
error=NO;
startedSection=NO;
// start the task
[task launch];
}
- (BOOL)isConcurrent {
return YES;
}
- (BOOL)isExecuting {
return isExecuting;
}
- (BOOL)isFinished {
return isFinished;
}
并在操作完成时执行以下代码:
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
isExecuting = NO;
isFinished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
当我[myQueue waitUntillAllOperationsAreFinished]
在将操作添加到任何操作之后添加时,myQueue
甚至都没有开始!当我删除时,[myQueue waitUntillAllOperationsAreFinished]
我得到了我想要的,除了这可能导致一些错误,因为主线程干扰了这些操作......