[英]iPhone - a delay between operations in the queue
我正在使用類似的東西向隊列中添加操作
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秒或者其他什么,所以不要運行
doStuff1
doStuff2
doStuff3
隊列會這樣做
doStuff1
sleep for X seconds
doStuff2
sleep for X seconds
doStuff3
?
謝謝。
使用睡眠是一個很大的浪費。 這在捉鬼敢死隊的意義上是不好的。
如果您需要一個非並發執行其操作的隊列,即順序執行,您可以簡單地將其maxConcurrentOperationCount變量設置為1。
queue.maxConcurrentOperationCount = 1;
要在操作之間暫停,您可以做兩件事:
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);
});
這將排除所有操作而不會阻塞。
b)每項操作都必須實施
- (BOOL)isFinished
你可能已經知道了。
如果您的操作“完成”,只需按照您所需的延遲延遲設置其“完成”變量。 使用類似的東西:
// 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"];
});
變體a)保證操作開始時間之間的暫停。 變體b)保證操作結束和下一個操作開始之間的暫停。
如果沒有額外的代碼,您無法保證您的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];
也就是說,如果不知道你需要發生什么,我想你或許只需要一個結合了所有三個任務的NSOperation並在它們之間放置一個睡眠。 那肯定是更簡單的代碼。
我有兩種方法可以做這樣的事情。
第一:讓當前線程在doStuff1,doStuff2和doStuff3的尾部休眠。
第二:子類NSOperation並覆蓋方法 - (void)main。 您可以使用以下參數初始化具有目標和操作參數的自定義NSOperation:
-(id)initWithTarget:(id)target action:(SEL)action;
並在 - (void)main中執行target的此操作
[target performSelector:action];
[NSThread sleepForTimeInterval:0.05];
你可以這樣做
[operation0 addDependancy:operation1];
[operation1 addDependancy:operation2];
現在在每個任務結束時添加[NSThread sleepforTimeInterval:5]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.