dispatch_after - GCD in Swift?

前端 未结 25 2383
心在旅途
心在旅途 2020-11-21 06:07

I\'ve gone through the iBook from Apple, and couldn\'t find any definition of it:

Can someone explain the structure of dispatch_after?

d         


        
相关标签:
25条回答
  • 2020-11-21 06:37

    Apple has a dispatch_after snippet for Objective-C:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        <#code to be executed after a specified delay#>
    });
    

    Here is the same snippet ported to Swift 3:

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + <#delayInSeconds#>) {
      <#code to be executed after a specified delay#>
    }
    
    0 讨论(0)
  • 2020-11-21 06:38

    Here is synchronous version of asyncAfter in Swift:

    let deadline = DispatchTime.now() + .seconds(3)
    let semaphore = DispatchSemaphore.init(value: 0)
    DispatchQueue.global().asyncAfter(deadline: deadline) {
        dispatchPrecondition(condition: .onQueue(DispatchQueue.global()))
        semaphore.signal()
    }
    
    semaphore.wait()
    

    Along with asynchronous one:

    let deadline = DispatchTime.now() + .seconds(3)
    DispatchQueue.main.asyncAfter(deadline: deadline) {
        dispatchPrecondition(condition: .onQueue(DispatchQueue.global()))
    }
    
    0 讨论(0)
  • 2020-11-21 06:40

    In Swift 4

    Use this snippet:

        let delayInSec = 1.0
        DispatchQueue.main.asyncAfter(deadline: .now() + delayInSec) {
           // code here
           print("It works")
        }
    
    0 讨论(0)
  • 2020-11-21 06:40

    use this code to perform some UI related task after 2.0 seconds.

                let delay = 2.0
                let delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
                let mainQueue = dispatch_get_main_queue()
    
                dispatch_after(delayInNanoSeconds, mainQueue, {
    
                    print("Some UI related task after delay")
                })
    

    Swift 3.0 version

    Following closure function execute some task after delay on main thread.

    func performAfterDelay(delay : Double, onCompletion: @escaping() -> Void){
    
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay, execute: {
           onCompletion()
        })
    }
    

    Call this function like:

    performAfterDelay(delay: 4.0) {
      print("test")
    }
    
    0 讨论(0)
  • 2020-11-21 06:41

    Swift 3+

    This is super-easy and elegant in Swift 3+:

    DispatchQueue.main.asyncAfter(deadline: .now() + 4.5) {
        // ...
    }
    

    Older Answer:

    To expand on Cezary's answer, which will execute after 1 nanosecond, I had to do the following to execute after 4 and a half seconds.

    let delay = 4.5 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), block)
    

    Edit: I discovered that my original code was slightly wrong. Implicit typing causes a compile error if you don't cast NSEC_PER_SEC to a Double.

    If anyone can suggest a more optimal solution I'd be keen to hear it.

    0 讨论(0)
  • 2020-11-21 06:44

    Swift 3.0 version

    Following closure function execute some task after delay on main thread.

    func performAfterDelay(delay : Double, onCompletion: @escaping() -> Void){
    
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay, execute: {
           onCompletion()
        })
    }
    

    Call this function like:

    performAfterDelay(delay: 4.0) {
      print("test")
    }
    
    0 讨论(0)
提交回复
热议问题