iPhone - 队列中的操作之间的延迟 [英] iPhone - a delay between operations in the queue
问题描述
我使用类似的东西向队列添加操作
I am adding operations to the queue using something like this
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];
队列设置为一次只执行一个操作。所以它会毫不拖延地一个接一个地运行3种方法。这是一种添加一个小延迟的方法,比如每次操作之间的0.5秒或其他任何操作,所以不要运行
The queue is set to do just one operation at a time. So it will run the 3 methods without delay, one after another. Is that a way to add a small delay, lets say 0.5 seconds or whatever between each operation, so instead of running
doStuff1
doStuff2
doStuff3
队列会做什么
doStuff1
sleep for X seconds
doStuff2
sleep for X seconds
doStuff3
?
谢谢。
推荐答案
没有额外的代码,你不能确保您的NSOperations将以串行方式运行 - 如果Apple发布多处理器iOS设备,它将并行运行多个操作。首先,您应该在操作之间设置依赖关系链。我建议您使用NSOperation的 - (void)addDependency:(NSOperation *)操作
来实现此目的。您也可以滚动自己的花式锁定代码...
Without extra code, you can't really guarantee that your NSOperations will run in serial - if Apple release a multiprocessor iOS device, it will run multiple operations in parallel. So firstly, you should set up the dependency chain between your operations. I would suggest that you achieve this using NSOperation's - (void)addDependency:(NSOperation *)operation
. You could also roll your own fancy locking code...
然后,您可以在每个doStuff方法的末尾添加一个sleep(5)。如果您需要在doStuff方法之外进行睡眠,请创建一个睡眠NSOperation:
You can then add a sleep(5) to the end of each of your doStuff methods. If you need the sleep outside the doStuff methods, create a sleep NSOperation:
- (void)sleepOperationBody; {
sleep(0.5f);
}
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(sleepOperationBody)
object:nil];
这就是说,如果不知道你需要发生什么,我想你或许只需要一个NSOperation结合了所有三个任务并在它们之间进行了睡眠。那当然是更简单的代码。
That said, without knowing in more detail what you need to happen, I think you perhaps just need one NSOperation that combines all three tasks and puts a sleep between them. That would certainly be simpler code.
这篇关于iPhone - 队列中的操作之间的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!