Hanushka Suren
Hanushka Suren

Reputation: 793

A function is not getting called inside a delegate method - IOS/Swift

I am developing an application with a custom camera and there I have used captureOutput method(Lets say A) which is a delegate method of AVCaptureVideoDataOutputSampleBufferDelegate.

I have used another function (Lets say B) which behaves like setTimeout in javascripts.

When I call B inside A it is not getting called.

Here is my code

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

    setTimeout(1) { () -> Void in         
        //piece of code i want to run               
    }
}

func setTimeout(delay:NSTimeInterval, block:()->Void) -> NSTimer {
    return NSTimer.scheduledTimerWithTimeInterval(delay, target: NSBlockOperation(block: block), selector: "main", userInfo: nil, repeats: false);
}

When I call setTimeout method inside viewDidLoad() it is working fine. But inside this particular method it is not getting called. Can someone give me a solution for this. Any help would be highly appreciated.

Edited: Confusing about the reason why my code is not working. Any idea?

Upvotes: 0

Views: 404

Answers (2)

Hanushka Suren
Hanushka Suren

Reputation: 793

I was able to find a way to get my work done.But couldn't find what is wrong with my previous code. This is how it looks like

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

    let seconds: Int64 = 1 * Int64(NSEC_PER_SEC);

    let time = dispatch_time(DISPATCH_TIME_NOW, seconds);

    dispatch_after(time, dispatch_get_main_queue(), {
        //piece of code i want to run
    });
}

Upvotes: 0

brimstone
brimstone

Reputation: 3400

This may be more suited to your purpose. It does what you're trying to do.

//From https://gist.github.com/natecook1000/b0285b518576b22c4dc8 by natecook1000
extension NSTimer {
    /**
     Runs a closure after a specified time

     - parameter delay:   The time before the `handler` is called
     - parameter handler: What happens after `delay` has passed

     - returns: The NSTimer
     */
    static public func schedule(delay delay: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
        let fireDate = delay + CFAbsoluteTimeGetCurrent()
        let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, 0, 0, 0, handler)
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
        return timer
    }

    /**
     Runs a closure after a specified time, forever

     - parameter interval: The time before the `handler` is called
     - parameter handler:  What happens after `delay` has passed

     - returns: The NSTimer
     */
    static func schedule(repeatInterval interval: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
        let fireDate = interval + CFAbsoluteTimeGetCurrent()
        let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, interval, 0, 0, handler)
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
        return timer
    }

}

Upvotes: 0

Related Questions