'dispatch_after equivalent in NSOperationQueue

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?

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

});


Solution 1:[1]

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.

Something like:

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

Solution 2:[2]

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

@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];

Solution 3:[3]

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

Solution 4:[4]

Swift 4:

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

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

Solution 5:[5]

Seven years late, but iOS 7 introduced this functionality to OperationQueue.

https://developer.apple.com/documentation/foundation/operationqueue/3329365-schedule

Solution 6:[6]

I used following code to get delayed call of operation:

class DelayedBlockOperation: Operation {
    private let deadline: DispatchTime
    private let block: (() -> Void)?
    private let queue: DispatchQueue

    override var isAsynchronous: Bool { true }
    override var isExecuting: Bool {
        get { _executing }
        set {
            willChangeValue(forKey: "isExecuting")
            _executing = newValue
            didChangeValue(forKey: "isExecuting")
        }
    }
    private var _executing: Bool = false

    override var isFinished: Bool {
        get { _finished }
        set {
            willChangeValue(forKey: "isFinished")
            _finished = newValue
            didChangeValue(forKey: "isFinished")
        }
    }
    private var _finished: Bool = false

    init(deadline: DispatchTime,
         queue: DispatchQueue = .global(),
         _ block: @escaping () -> Void = { }) {
        self.deadline = deadline
        self.queue = queue
        self.block = block
    }

    override func start() {
        queue.asyncAfter(deadline: deadline) {
            guard !self.isCancelled else {
                self.isFinished = true
                return
            }
            guard let block = self.block else {
                self.isFinished = true
                return
            }
            self.isExecuting = true
            block()
            self.isFinished = true
        }
    }
}

I was inspired by the gist

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan Grynspan
Solution 2 battlmonstr
Solution 3
Solution 4 alegelos
Solution 5 Chris Hinkle
Solution 6 Constantine Nikolsky