iPhone - 队列中的操作之间的延迟

Spa*_*Dog 4 iphone

我正在使用类似的东西向队列中添加操作

NSInvocationOperation *operation0 = [[NSInvocationOperation alloc] 
initWithTarget:self
selector:@selector(doStuff1) 
object:nil];

[queue addOperation:operation0];
[operation0 release];   


NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] 
initWithTarget:self
selector:@selector(doStuff2) 
object:nil];

[queue addOperation:operation1];
[operation1 release];   


NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] 
initWithTarget:self
selector:@selector(doStuff3) 
object:nil];

[queue addOperation:operation2];
[operation2 release];   
Run Code Online (Sandbox Code Playgroud)

队列设置为一次只执行一个操作.所以它会毫不拖延地一个接一个地运行3种方法.这是一种添加一个小延迟的方法,比如说每次操作之间的0.5秒或者其他什么,所以不要运行

doStuff1
doStuff2
doStuff3
Run Code Online (Sandbox Code Playgroud)

队列会这样做

doStuff1
sleep for X seconds
doStuff2
sleep for X seconds
doStuff3
Run Code Online (Sandbox Code Playgroud)

谢谢.

Mac*_*ark 11

使用睡眠是一个很大的浪费.这在捉鬼敢死队的意义上是不好的.

如果您需要一个非并发执行其操作的队列,即顺序执行,您可以简单地将其maxConcurrentOperationCount变量设置为1.

queue.maxConcurrentOperationCount = 1;
Run Code Online (Sandbox Code Playgroud)

要在操作之间暂停,您可以做两件事:

a)延迟每个操作的排队:

//loop over the operations to be queued with and index
      double delayInSeconds = 2.0 * index;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            queue.addOperation(operation at index); 
        });
Run Code Online (Sandbox Code Playgroud)

这将排除所有操作而不会阻塞.

b)每项操作都必须实施

- (BOOL)isFinished
Run Code Online (Sandbox Code Playgroud)

你可能已经知道了.

如果您的操作"完成",只需按照您所需的延迟延迟设置其"完成"变量.使用类似的东西:

// Operation is done with its work, but we tell the Queue only after 2 seconds, so the queue will start the next operation later
double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
 __weak __typeof(self)weakSelf = self;
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[weakSelf willChangeValueForKey:@"isExecuting"];
    [weakSelf willChangeValueForKey:@"isFinished"];
    weakSelf.executing = NO;
    weakSelf.finished  = YES;
    [weakSelf didChangeValueForKey:@"isFinished"];
    [weakSelf didChangeValueForKey:@"isExecuting"];
    });
Run Code Online (Sandbox Code Playgroud)

变体a)保证操作开始时间之间的暂停.变体b)保证操作结束和下一个操作开始之间的暂停.


Rog*_*Rog 6

如果没有额外的代码,您无法保证您的NSOperations将以串行方式运行 - 如果Apple发布多处理器iOS设备,它将并行运行多个操作.首先,您应该在操作之间设置依赖关系链.我建议您使用NSOperation来实现这一目标- (void)addDependency:(NSOperation *)operation.你也可以推出自己的花式锁定代码......

然后,您可以在每个doStuff方法的末尾添加sleep(5).如果您需要在doStuff方法之外进行睡眠,请创建一个睡眠NSOperation:

- (void)sleepOperationBody; {
sleep(0.5f);
}

NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] 
initWithTarget:self
selector:@selector(sleepOperationBody) 
object:nil];
Run Code Online (Sandbox Code Playgroud)

也就是说,如果不知道你需要发生什么,我想你或许只需要一个结合了所有三个任务的NSOperation并在它们之间放置一个睡眠.那肯定是更简单的代码.

  • >如果没有额外的代码,您无法保证您的NSOperations将以串行方式运行.嗯,是的你可以,对吧?只需将NSOperationQueue上的maxConcurrentOperations设置为1.那就行了,不是吗? (6认同)