xcode NSOperationQueue 中的 dispatch_after 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15480618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:00:20  来源:igfitidea点击:

dispatch_after equivalent in NSOperationQueue

iosobjective-cxcodegrand-central-dispatchnsoperationqueue

提问by sinθ

I'm moving my code from regular GCD to NSOperationQueuebecause 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?

我正在将我的代码从常规 GCD 移动到NSOperationQueue因为我需要一些功能。我的很多代码都依赖 dispatch_after 才能正常工作。有没有办法用 a 做类似的事情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.

这是我的一些需要转换为NSOperation. 如果您能提供一个使用此代码进行转换的示例,那就太好了。

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)];
    }

});

回答by gaige

NSOperationQueuedoesn'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 NSOperationfrom the dispatch_afterin order to handle both the delay and making the final code an NSOperation.

NSOperationQueue里面没有任何计时机制。如果您需要像这样设置延迟然后执行操作,您需要调度NSOperationfromdispatch_after以处理延迟并使最终代码成为NSOperation.

NSOperationis 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.

NSOperation旨在处理或多或少的批处理操作。用例与 GCD 略有不同,实际上在具有 GCD 的平台上使用 GCD。

If the problem you are trying to solve is to get a cancelable timer notification, I'd suggest using NSTimerand 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.

如果您尝试解决的问题是获得可取消的计时器通知,我建议NSTimer您在需要取消时使用并使其无效。然后,为了响应计时器,您可以执行您的代码,或者使用调度队列或 NSOperationQueue。

回答by Jonathan Grynspan

You can keep using dispatch_after()with a global queue, then schedule the operation on your operation queue. Blocks passed to dispatch_after()don't execute after the specified time, they are simply scheduled after that time.

您可以继续使用dispatch_after()全局队列,然后在您的操作队列上安排操作。传递给的块dispatch_after()在指定时间之后不会执行,它们只是在该时间之后安排。

Something like:

就像是:

dispatch_after
(
    mainPopTime,
    dispatch_get_main_queue(),
    ^ {
        [myOperationQueue addOperation:theOperationObject];
    }
);

回答by battlmonstr

You could make an NSOperation that performs a sleep: MYDelayOperation. Then add it as a dependency for your real work operation.

您可以创建一个执行睡眠的 NSOperation:MYDelayOperation。然后将其添加为您实际工作操作的依赖项。

@interface MYDelayOperation : NSOperation
...
- (void)main
{
    [NSThread sleepForTimeInterval:delay]; // delay is passed in constructor
}

Usage:

用法:

NSOperation *theOperationObject = ...
MYDelayOperation *delayOp = [[MYDelayOperation alloc] initWithDelay:5];
[theOperationObject addDependency:delayOp];
[myOperationQueue addOperations:@[ delayOp, theOperationObject] waitUntilFinished:NO];

回答by Bogdan

[operationQueue performSelector:@selector(addOperation:) 
                     withObject:operation 
                     afterDelay:delay];

回答by alegelos

Swift 4:

斯威夫特 4:

DispatchQueue.global().asyncAfter(deadline: .now() + 10 { [weak self] in
                guard let `self` = self else {return}

                self. myOperationQueue.addOperation {
                    //...code...
                }
            }