【发布时间】:2015-03-13 19:58:09
【问题描述】:
在我的代码中,我想在队列中单独处理操作,并能够暂停和恢复队列中的操作操作。我该如何实现呢?
让我简要解释一下我的问题,我使用下面的代码通过子类 NSOperation 创建操作并将此操作添加到 NSOperationQueue。
@interface MyLengthyOperation: NSOperation
@end
@implementation MyLengthyOperation
- (void)main
{
// a lengthy operation
@autoreleasepool
{
for (int i = 0 ; i < 10000 ; i++)
{
// is this operation cancelled?
if (self.isCancelled)
break;
NSLog(@"%f", sqrt(i));
}
}
}
@end
我已经创建了上面的操作,我用
调用它NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
myQueue.name = @"Download Queue";
myQueue.maxConcurrentOperationCount = 4;
[myQueue addOperation:operation];
现在我想暂停并恢复我的操作。假设循环在 6786 上,我按下了暂停按钮,那么队列应该暂停这个操作,当我按下开始时,它应该从 6786 开始。
我想控制队列中的多个操作。我该怎么做?
我查看了[queue setSuspended:YES]; - 这将阻止队列添加新操作,但我想控制队列中现有的操作。
等待您的答复。
提前致谢!
【问题讨论】:
标签: ios multithreading nsoperationqueue