在NSOperationQueue中的dispatch_after等价物 [英] dispatch_after equivalent in NSOperationQueue
问题描述
我正在将我的代码从常规GCD转移到 NSOperationQueue
,因为我需要一些功能。我的很多代码都依赖dispatch_after才能正常工作。有没有办法用 NSOperation
做类似的事情?
I'm moving my code from regular GCD to NSOperationQueue
because I need some of the functionality. A lot of my code relies on dispatch_after in order to work properly. Is there a way to do something similar with an NSOperation
?
这是我需要的一些代码转换为 NSOperation
。如果您可以提供使用此代码转换它的示例,那就太棒了。
This is some of my code that needs to be converted to NSOperation
. If you could provide an example of converting it using this code, that would be great.
dispatch_queue_t queue = dispatch_queue_create("com.cue.MainFade", NULL);
dispatch_time_t mainPopTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeRun * NSEC_PER_SEC));
dispatch_after(mainPopTime, queue, ^(void){
if(dFade !=nil){
double incriment = ([dFade volume] / [self fadeOut])/10; //incriment per .1 seconds.
[self doDelayFadeOut:incriment with:dFade on:dispatch_queue_create("com.cue.MainFade", 0)];
}
});
推荐答案
NSOperationQueue
中没有任何计时机制。如果您需要设置这样的延迟然后执行操作,您需要从 dispatch_after $ c安排
NSOperation
$ c>为了处理延迟并使最终代码成为 NSOperation
。
NSOperationQueue
doesn't have any timing mechanism in it. If you need to set up a delay like this and then execute an operation, you'll want to schedule the NSOperation
from the dispatch_after
in order to handle both the delay and making the final code an NSOperation
.
NSOperation
旨在处理或多或少的批处理操作。用例与GCD略有不同,实际上在使用GCD的平台上使用GCD。
NSOperation
is designed to handle more-or-less batch operations. The use case is slightly different from GCD, and in fact uses GCD on platforms with GCD.
如果您要解决的问题是获取可取消的计时器通知,我建议使用 NSTimer
如果您需要取消它,它将使其无效。然后,为响应计时器,您可以执行代码,或使用调度队列或NSOperationQueue。
If the problem you are trying to solve is to get a cancelable timer notification, I'd suggest using NSTimer
and invalidating it if you need to cancel it. Then, in response to the timer, you can execute your code, or use a dispatch queue or NSOperationQueue.
这篇关于在NSOperationQueue中的dispatch_after等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!